0

Ajax Code

$.ajax({
    type: "POST",
    url: "LiveFeed.aspx/SearchStateList",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        $("#Content").text(response.d);
    },
    failure: function (response) {
        alert(response.d);
    }
});

Code Behind

[WebMethod]
public static string SearchStateList()
{

}

The above code works fine and the code behind is called

But when I send some parameter as given below, code behind is not invoked and when I see the Firebug console errors, it throws

NetworkError: 500 Internal Server Error - http://localhost:61276/App/LiveFeed/LiveFeed.aspx/SearchStateList

Ajax Code

$.ajax({
    type: "POST",
    url: "LiveFeed.aspx/SearchStateList",
    contentType: "application/json; charset=utf-8",
    data:{value:"samplevalue"},
    dataType: "json",
    success: function (response) {
        $("#Content").text(response.d);
    },
    failure: function (response) {
        alert(response.d);
    }
});

Code Behind

[WebMethod]
public static string SearchStateList(string value)
{

}

I also tried to change modify the data of ajax call as

var param={value:"samplevalue"}

data:JSON.stringify(param),

and also tried directly with out JSON.stringify

data:param,

Every time when I tried to pass a parameter it did not invoke code behind and 500 error is thrown.

Gopi
  • 5,656
  • 22
  • 80
  • 146

4 Answers4

3

Why don't you try

var param={"value":"samplevalue"}

data:JSON.stringify(param),
wherby
  • 704
  • 1
  • 5
  • 16
0

I think data content goes with quotes:

data: '{value:"samplevalue"}',
Alex Coloma
  • 651
  • 3
  • 8
  • You never should manually build your own JSON string - `JSON.stringify` will do the work for you, including escaping out special characters as needed. If you do it yourself like this, you open yourself up to mistakes or parsing errors that you may not catch in testing. – Joe Enos Jun 03 '15 at 15:56
  • Also, this wouldn't be valid JSON, because in JSON (unlike javascript object literals), the keys must be quoted. – Joe Enos Jun 03 '15 at 15:58
  • Ok, thanks for the info – Alex Coloma Jun 03 '15 at 15:59
-1

You can access variables sent in the data by accessing the Request Collection.

Request.Form["your_var_name"]  // use this when you $.post
Request["your_var_name"] // use this when you $.get

So your code behind can look like this:

public static string SearchStateList(string value)
{
    Response.Write("The value of value is: " + Request.Form["value"]);
}
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
-1

In your Ajax post, you have set the data property as :-

data:{value:"samplevalue"}  

Subtle change, to this below would work :-

 data:{ "value" :"samplevalue"}
Derek
  • 8,300
  • 12
  • 56
  • 88
  • Would like to know why the down vote. – Derek Jun 03 '15 at 15:58
  • You didn't provide any explanation of what you were suggesting or why it would make a difference, and what you put in there is exactly what they already tried in the question (quoting the key in a javascript object literal doesn't change anything, so this is exactly the same as `data:{value:"samplevalue"}`). – Joe Enos Jun 03 '15 at 16:09
  • look again, the user tried :- data:{value:"samplevalue"} - In his ajax post. I suggested data:{ "value" : "samplevalue" }. Point Noted about the lack of explanation. – Derek Jun 03 '15 at 16:11
  • As I said, just quoting the key in a javascript literal doesn't change anything. `data:{value:"samplevalue"}` and `data:{ "value" :"samplevalue"}` are exactly the same thing in javascript. Remember that a javascript object literal is not the same thing as JSON - javascript object literals don't require quoted keys. – Joe Enos Jun 03 '15 at 16:41