timu
timu

Reputation: 838

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 testMethod(string test)
{
     return test;
}

Upvotes: 4

Views: 1792

Answers (2)

Dave Ward
Dave Ward

Reputation: 60580

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).

Upvotes: 0

Erix
Erix

Reputation: 7105

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>

Upvotes: 2

Related Questions