Reputation: 31
Here is my webmethod in a web service (local to the sample project) .asmx
[WebMethod]
public List<test1> GetLstB(string s)
{
test1 t;
List<test1> lstB = new List<test1>();
t = new test1()
{
itm1 = "aaa" + s,
itm2 = "bbb" + s
};
lstB.Add(t);
t = new test1()
{
itm1 = "ccc" + s,
itm2 = "ddd" + s
};
lstB.Add(t);
return lstB;
}
public class test1
{
public string itm1 { get; set; }
public string itm2 { get; set; }
}
and here is the JQuery that calls this webmethod
$(function () {
$("[id*=Button4").click(function () {
var x = 'xyz';
$.ajax({
type: "POST",
url: "ServiceCS.asmx/GetLstB",
data: "{'" + x + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
for (var i in result.d) {
alert(result.d[i].itm1 + "--" + result.d[i].itm2);
}
},
error: function (r) {
alert(r.responseText);
console.log("AJAX error in request: " + JSON.stringify(r, null, 2));
},
failure: function (r) {
alert(r.responseText);
}
});
return false;
});
});
I ran this JQuery function without passing a parameter (var x = 'xyz';) and the webmethod returned the object list which I iterated through in JQuery without any problems. But then I decided to try passing a parameter to the webmethod (also tried "xyz") and now I get this error -- how do I pass the parameter var x = "xyz"; to the webmethod correctly?
"Message\":\"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (7): {\u0027xyz\u0027}\",\"StackTrace\":\" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodDat
Upvotes: 0
Views: 10370
Reputation: 1731
above both answers are still clueless for the beginners. as javaScript and jquery is case sensitive.
the correct syntax to pass arguement is
data: "{'s':'"+x+"'}"
Upvotes: 1
Reputation: 888
The parameter should be like this.
$(function () {
$("[id*=Button4").click(function () {
var x = 'xyz';
$.ajax({
type: "POST",
url: "ServiceCS.asmx/GetLstB",
data: "{'s:" + x + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
for (var i in result.d) {
alert(result.d[i].itm1 + "--" + result.d[i].itm2);
}
},
error: function (r) {
alert(r.responseText);
console.log("AJAX error in request: " + JSON.stringify(r, null, 2));
},
failure: function (r) {
alert(r.responseText);
}
});
return false;
});
});
Or you can do is :
data:JSON.stringify({ s: x})
Upvotes: 0
Reputation: 440
$(function () {
$("[id*=Button4]").click(function () {
var x = 'xyz';
$.ajax({
type: "POST",
url: "ServiceCS.asmx/GetLstB",
data: {s:x},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
for (var i in result.d) {
alert(result.d[i].itm1 + "--" + result.d[i].itm2);
}
},
error: function (r) {
alert(r.responseText);
console.log("AJAX error in request: " + JSON.stringify(r, null, 2));
Upvotes: 0