'jQuery ajax dataType: "json"

Have gone through related questions but none answers my problem.

I'm using asp.net 3.5 and jQuery 1.9. dataType: "json" gives a parser error. If I have it removed or using "text", I'm not able to read string returned from webmethod.

Please help me with an example of a jQuery ajax script to call the method below and display the returned string.

    [WebMethod]
    public static string MyWebMethod(string parm1, string parm2)
    {
        return "success from webmethod";
    }

Sorry everybody, it's my mistake. I used "static" in the web method of the web service. Method was not discoverable.



Solution 1:[1]

Your return string is not in json format, so it is not surprising you get a parse error. Your return type is a non-formatted string, so a dataType:"text" should work, e.g. something like the following:

$.ajax("/some/script.asp", {dataType:"text", ....}) 

You should examine what your asp script is actually returning with something like:

  $.ajax(
    "/your_script.asp", 
    {
      dataType:"text",
      complete: function(result, success) {
                   console.log(result, success);  //ADD THIS*****
                }
    }
  );

Solution 2:[2]

$.ajax({
    type: "POST",
   url: '@Url.Action("Action","Controller")',
    data: "{}",
    async: true,
    dataType: "text",
    success: function( data ) {
        alert(data)
    }
});

Solution 3:[3]

Could you return something like

public static ActionResult MyWebMethod(string parm1, string parm2)
{
    return Json(new { text= "success from webmethod"}, JsonRequestBehavior.AllowGet);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 7stud
Solution 2 Krishan Pal
Solution 3 Nico