4

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;
}
timu
  • 828
  • 7
  • 20
  • 40
  • Use firebug for firefox or the develop console in IE or Chrome to take a look at the exact request and response should give you the clue your looking for. – RubbleFord Aug 15 '11 at 14:16
  • Response is page source which is server returns. – timu Aug 15 '11 at 14:18
  • Is this page content, a custom error page maybe? I can't see anything obvious wrong with what your doing. – RubbleFord Aug 15 '11 at 14:20
  • 1
    See this question: http://stackoverflow.com/questions/583116/call-asp-net-pagemethod-webmethod-with-jquery-returns-whole-page – Russ Clarke Aug 16 '11 at 13:21

2 Answers2

2

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>
Erix
  • 7,059
  • 2
  • 35
  • 61
0

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

Dave Ward
  • 59,815
  • 13
  • 117
  • 134