I got this error when i send 2 parameter from jQuery to WebMethod and using multiple params.
{"Message":"Invalid web service call, missing value for parameter: u0027hahau0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)rn at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)rn at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)rn at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
In jQuery:
$(".txtNoiDung").focusout(function () {
$.ajax({
type: "POST",
url: "QuanLyTin.aspx/test1cai",
data: JSON.stringify({ hahas: $(this).val(),tuans: "hahaha" }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#vltxtNoiDung").text(msg.d)
},
error: function (xhr, reason, ex) {
alert(reason);
}
});
});
In code behind
[WebMethod()]
public static string test1cai(string haha, string tuan)
{
return "Hi, "+haha + tuan;
}
How can i resolve it?
Thanks you guys.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Your service is accepting parameters named haha and tuan, but your JavaScript is passing in hahas and tuans. Remove the “s” from both:
data: JSON.stringify({ haha: $(this).val(),tuan: "hahaha" }),
Also, keep in mind that these parameters much match between client- and server-side with case-sensitivity.
Method 2
Your JavaScript object property names must match the names of the parameters on the web service method so they can be bound appropriately. You currently have:
{ hahas: $(this).val(),tuans: "hahaha" }
which should probably be:
{ haha: $(this).val(), tuan: "hahaha" }
Method 3
You should be passing the same method parameter from the function in code behinedto, in your Ajax call
data: “{ ‘haha’: ‘” + “your data” + ‘tuan’: ‘” + “your data” + “‘ }”
- There shouldn’t be any spaces like ‘ tuan ‘
Method 4
Basically you have to use curly brackets while JSON.stringify({})
You have to use like this :
$.ajax({
type: “POST”,
url: url,
data: JSON.stringify({ PrefrencesDetailEntity: PrefrencesDetailEntity
contentType: “application/json;”, //charset=utf-8″,
dataType: “json”,
success: function (data, jqXHR) {
if (data !== null && data.Status === 201) {
toastr.success(“Your changes have been saved successfully”);
}
}
});
and C# method would be like this :
[WebMethod]
public static NotificationValues PostNotificationData(PrefrencesDetailEntity PrefrencesDetailEntity)
{
//your C# code logic
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0