6

I'm about to pull out any remaining hair that I have, so please help me out if you know what the problem might be... Thanks. All my googling and searching has not paid off either.

First, I'm using jquery-1.7.2.min.js and ASP.net 2.0 web form.

I'm trying to make an ajax call using jquery but keep getting syntax error/parse error messages. I've tried many different ways but they all result in the error when I set the dataType to json.

Here's what I have:

$.ajax({
    type: "POST",
    url: "UserList.aspx/GetTestJson",
    data: {}, //have also tried "{}" and other options such as removing the line
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(data, textStatus, jqXHR){
        alert('success');
    },
    //Using below instead of above makes no difference either
    //success: function(data){
    //  alert('success');
    //},
    error: function(jqXHR, textStatus, errorThrown){
        alert('errorThrown: ' + errorThrown);
    }
});

and the aspx page method:

[WebMethod]
public static string GetTestJson()
{
    return "Hello My World";
}

I've tried setting up a web service too, but get the same result. It appears that the response coming back is either the full html for the page or xml from the web service and because I set the dataType to json it is not successful in parsing it. If this is the case, how do I set the response to only return json?

Here is something else I've tried without luck:

[WebMethod]
public static string GetTestJson()
{
    HttpContext.Current.Response.ContentType = "application/json";
    string json = JavaScriptConvert.SerializeObject("Hello My World");
    return json;
}

Error messages I get through jQuery:

errorThrown.message = "Syntax error"
errorThrown.number = -2146827286
errorThrown.name = "SyntaxError"
textStatus = "parseerror"
jqXHR.status = 200
jqXHR.statusText = "OK"
jqXHR.readyState = 4
jqXHR.responseText = (the complete html of the page)

Any ideas or suggestions?

Thanks

PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44
  • Merely setting the Content-type HTTP header does not actually magically change the content of the response. You need to convert data to JSON yourself. Also, [WebMethod] is usually used for SOAP services, not RESTful-style web-services. – Dai Jul 31 '12 at 23:10
  • 1
    That's partially true, PageMethods are used specifically in AJAX scenarios, they set the response type to json by default – Jupaol Jul 31 '12 at 23:12
  • Can you post the exact error you are receiving – Jupaol Jul 31 '12 at 23:13
  • @Jupaol I've added some more details. I have also tried various versions of jQuery's data options such as "{}" or even removing it. Again, no luck... (thanks for the quick responses) – PostureOfLearning Jul 31 '12 at 23:26
  • 1
    What version of ASP.NET are you using? – Dave Ward Aug 01 '12 at 01:01
  • 2
    @Rob: It sounds like you don't have the ASP.NET Ajax Extensions installed or your web.config isn't updated to take advantage of those new features (including the static "Page Method" feature you're trying to use here). I've written a bit about how to get that set up here: http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/ – Dave Ward Aug 01 '12 at 03:51
  • 2
    Hi @Rob. Try this example I just written. As Dave just mentioned the test code needs the web.config correctly configured, check the code (with a working web.config) on here: http://www.mediafire.com/?5l7va36edkc3tkl – CoderRoller Aug 01 '12 at 03:54
  • If you debug it in firebug and look at the reponse what is actually being returned? – Daniel Powell Aug 01 '12 at 22:32
  • Thanks to DaveWard and @CodeRoller I finally managed to get this working. (And I still have some hair left :)) If you put up an answer similar to your comments I'll give you some points too ;) Thanks a lot guys. – PostureOfLearning Aug 01 '12 at 22:59
  • Thank you @DaveWard and CodeRoller – PostureOfLearning Aug 01 '12 at 22:59

2 Answers2

12

Thank you @DaveWard and @CodeRoller for your help which led to this solution:

  1. Install ASP.NET Ajax Extensions (HERE)
  2. Add reference to System.Web.Extensions.dll
  3. Modify web.config to include the ScriptModule httpModule (see below)

My jQuery ajax call and the WebMethod are still the same as my original post.

My web.config has been modified as below:

  <system.web>
    <compilation debug="true">
      <assemblies>
        <!-- A bunch of other assemblies here-->
        <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </httpModules>
  </system.web>

I hope someone else will find this useful.

PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44
-1

You should call a function inside PageLoad Function

If(!Page.IsPostback){
   ...
}
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118