I am passing a javascript variable containing a string to the server via jquery.ajax. Although the “success” condition is called, the server-side WebMethod is never called. Client:
$.ajax({
type: "post",
url: "Playground.aspx/childBind",
data: {sendData: ID},
//contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (result) { alert("successful!" + result.d); }
})
Server:
[WebMethod]
public static string childBind(string sendData)
{
return String.Format("Hello");
}
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
Try following fixes for your Ajax request:
$.ajax({
type: "post",
url: "Playground.aspx/childBind",
data: "{sendData: '" + ID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) { alert("successful!" + result.d); }
})
Notice changed dataType and data value as a string.
Method 2
I have encountered the same issue. After Googling, I found the solution, and it works for me. Navigate to RouteConfig.cs and comment out the line below:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
Method 3
i would want to add one note: you will have data error of your “ID” (or another field) string contains quotes like = ‘.
solve this issue:
var DTO = {'sendData': ID};
$.ajax({
"type": "POST",
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"url": sSource,
"data": JSON.stringify(DTO),
"success": function (msg) {
//do something
}
});
Method 4
Try like this:
JQuery:
var dataString = JSON.stringify({
contractName: contractName,
contractNumber: contractNumber
});
$.ajax({
type: "POST",
url: "CreateQuote.aspx/GetCallHistory",
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result);
OpenLightBox('divDelete');
}
});
ASPX.CS:
[System.Web.Services.WebMethod]
public static string GetCallHistory(string contractName, string contractNumber)
{
return "Nalan";
}
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