I am passing a javascript variable containing a string to the server via j开发者_如何学Goquery.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");
}
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.
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);
}
}
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
}
});
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";
}
精彩评论