I have an .Ajax method that calls a method on the page behind (does some logic, then calls a WCF service, then returns an object) that looks like this:
$.ajax({
type: 'POST',
url: 'MyPage.aspx/TestMethod',
dataType: "json",
contentType: "application/json",
data: "{'Id': '" + Id + "'}",
success: function (data) {
$("#randomElement").html(data.d.Foo);
},
error: function ((xhr, textStatus, error){
alert(xhr.statusText);
alert(textStatus);
alert(error);
});
And the page behind:
[System.Web.Services.WebMethod]
public static MyObject TestMethod(string Id)
{
//Logic removed for readability!
return MyService.GetStuff(Id);
}
This works perfectly fine on my local machine, but fails to work when I deploy to my server.
I have determined that the page behind method does not get called (ie the error is in calling TestMethod(), not TestMethod() calling the service).
I have also placed an alert before the .ajax method to show me the path of the page I am on just in case it was doing something funny with the uri on the live server, and it comes back as "/MyPage.aspx" as I would expect.
I also changed the "url:" to the full uri to see if that helped and it didn't.
After running fiddler with custom errors off, I get the following error:
{"Message":"Thread was being aborted.","StackTrace":" at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)\\r\\n at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)\\r\\n at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)\\r\\n at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)\\r\\n at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary
2 parameters)\\r\\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary
2 parameters)\\r\\n at System.Web.Script. Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\\r\\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.Threading.ThreadAbortException"}
Your JSON is not valid; you need to use double quotes:
data: '{"Id": "' + Id + '"}',
To me it seems like the it is not posting the right parameters to your method.
I've never used the data option in the .ajax method like you do here before. I use:
data: {Id: myId},
Where myId is the variable holding the value of the Id you want to pass - without any quotes.
the \\r\\n is usually something in relation to encoding strings which is not something that should be relevant here, and it seems like you are passing a string data: "{'Id': '" + Id + "'}" instead of an object.
Does your server has a "json"? godaddy, for example, does not offer it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.