PageMethod returns entire page
Hi we are using jquery to sending ajax requests but its returns page's content everytime. We are using .NET Framework version 2
$.ajax({
type: "POST",
url: "ajaxPage.aspx/testMethod",
data: "{test:'test'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
$("#span_result").html(result.d).fadeIn();
},
error: function (msg) {
$("#span_result").hide();
}
});
//ajaxPage.aspx.cs
[System.Web.Services.WebMethod]
public static string t开发者_C百科estMethod(string test)
{
return test;
}
Do you have this in your web.config?
<system.web>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web>
The ScriptModule that SP suggested is probably what you're missing.
One other thing is that your data parameter isn't valid. I don't think that would cause the issue you're seeing now, but it may start causing an invalid JSON primitive error once you've fixed the current problem. Change it to this:
data: '{"test":"test"}'
The key names must always be quoted, and the quotes around JSON keys and values should be double quotes (though ASP.NET is more forgiving about that latter point).
精彩评论