views:

172

answers:

3

Hi, I have to pass string entered in the input text to server method calling through jquery ajax. But its not going through. can please somebody tell me what i m doing wrong here. Below is the code:

$.ajaxSetup({    cache: false    timeout: 1000000}); function concatObject(obj) {    strArray = []; //new Array    for (prop in obj) {        strArray.push(prop + " value :" + obj[prop]);    }    return strArray.join();} //var Eid = "stephen.gilroy1"; function testCAll() {    //var ntid = $('#Eid').val();     $.ajax({        type: "POST",        url: "Testing.aspx/SendMessage",        //data: "{'ntid':'stephen.gilroy1'}",       //working        data: "{'ntid': $('#Eid').val()}",        contentType: "application/json; charset=utf-8",        dataType: "json",        success: function(result) {            alert(result.d);            resultData = eval("(" + result.d + ")");            $("#rawResponse").html(result.d);            //$("#response").html(resultData.sn);        },        error: function(result) {            alert("jQuery Error:" + result.statusText);        }    });}$.ajaxSetup({
    cache: false
    //timeout: 1000000
});
function concatObject(obj) {
    strArray = []; //new Array
    for (prop in obj) {
        strArray.push(prop + " value :" + obj[prop]);
    }
    return strArray.join();
}

//var Eid = "stephen.gilroy1";

function testCAll() {
    //var ntid = $('#Eid').val(); 
    $.ajax({
        type: "POST",
        url: "Testing.aspx/SendMessage",
        //data: "{'ntid':'stephen.gilroy1'}",       //working
        data: "{'ntid': $('#Eid').val()}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            alert(result.d);
            resultData = eval("(" + result.d + ")");
            $("#rawResponse").html(result.d);
            //$("#response").html(resultData.sn);
        },
        error: function(result) {
            alert("jQuery Error:" + result.statusText);
        }
    });
}

above is js file and below is its aspx file:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Testing.aspx.cs" Inherits="Testing" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
    <script src="jquery.js" type="text/javascript"></script>

    <script src="Testing.js" type="text/javascript"></script>

    <script src="json2.js" type="text/javascript"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    Employee's NTID: <input type="text" id = "Eid" name="Employee_NTID" />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />
<br />
     <input type="button" onclick="testCAll()" value = "Search"/>

       <div id="rawResponse"></div>
       <hr />
       <div id="response"></div>

    </div>

    </form>
</body>
</html>
A: 

You need to take data: "{'ntid': $('#Eid').val()}", out of quotes.

EDIT Take a look here, the code that's in your question properly attempts to send an ajax request.

EDIT 2 Here you go:

$.ajax({
    type: "POST",
    url: "Testing.aspx/SendMessage",
    data: { ntid: $('#Eid').val()}, // Notice the lack of quotes
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result) {
        console.log(result.d);
        resultData = eval("(" + result.d + ")");
        //$("#rawResponse").html(result.d);
    },
    error: function(result) {
        alert("jQuery Error:" + result.statusText);
    }
});
R0MANARMY
Thanks but problem is still here
amby
@amby: what specifically isn't working. As the link I provided shows, the AJAX call is being fired properly.
R0MANARMY
i know that my code successfully send ajax request because i tried sending just simple string likedata: "{'ntid':'stephen.gilroy1'}", and it worked fine but when i try to send input textbox string then it gives jquery:internal server error
amby
@amby: Did you look at the link I provided? You should also update the code in your question to reflect mine and Jonathan Mayhak comment about using an object instead of a string as your **data** argument?
R0MANARMY
i could not see ur code there. can u please copy ur code here so that i can take look at it.
amby
A: 

data isn't formatted right. You can create the json object outside of the ajax() function to make it easier to manage.

    var keyvalue = {
      ntid : $('#Eid').val()
    };
    $.ajax({
            type: "POST",
            url: "Testing.aspx/SendMessage",
            //data: "{'ntid':'stephen.gilroy1'}",       //working
            data: keyvalue,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result) {
                alert(result.d);
                resultData = eval("(" + result.d + ")");
                $("#rawResponse").html(result.d);
                //$("#response").html(resultData.sn);
            },
            error: function(result) {
                alert("jQuery Error:" + result.statusText);
            }
    });
Jonathan Mayhak
thanks but still not solved :(...any other idea
amby
are you getting a javascript error?
Jonathan Mayhak
jquery internal server error, but the thing is that when i try to send data like this: data: "{'ntid':'stepqwe1'}", its working but when try to send input text value, then its giving above error
amby
A: 

Thanks guys! its working now by using this line: data: "{'ntid': '"+$('#Eid').val()+"'}",

amby