2

Im ready to start pulling my hair out here. Trying to use jquery ajax request in Firefox to return simple string from method in my code behind. Regardless of what I try, I always get parsererror - unexpected character. I tried at least a dozen different variations based on demos Ive found online, none work.

 $.ajax({
                type: 'POST',
                url: '/Search/BasicSearch.aspx/sayHello',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: '{}',
                success: function (msg) {
                    alert(msg.d);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus + '  ' + errorThrown);
                }
            });

--code behind method

[WebMethod]
        public static string sayHello()
        {
            return "hello world";
        }

I have tried returning a properly formatted json string in this method, still didnt work.

EDIT: I forgot to mention that this site will is running on .NET 2.0. After unsuccessfully trying Will's suggestion below to set the response format to JSON, I thought I would try a plain 'ol generic handler and see what happens. Sure enough, it worked.

public class Handler1 : IHttpHandler {

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/json";
    context.Response.Write("{\"\": \"hello world\"}");
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

}

So I am assuming its an underlying issue with .NET 2.0 aspx pages??? I think all of the examples I have seen were using at least 3.0.

user163757
  • 6,795
  • 9
  • 32
  • 47
  • Have you tried adding the ScriptMethod attribute to sayHello method? Webmethods are SOAP calls by default, returning XML. eg `[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]` – Will Jul 03 '11 at 00:54
  • Ah, ScriptMethod's are only supported in 3.5+. You need to either install 3.5 (which is backwards compatible with 2.0), or hack around the issue, probably as you've shown using HttpHandlers. Other hits: http://stackoverflow.com/questions/288850/how-to-return-json-from-a-2-0-asmx-web-service , http://www.codeproject.com/KB/webservices/jsonwebservice.aspx , http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/ – Will Jul 03 '11 at 01:26
  • @user , i had the same issue few days back , mostly it is due to your returned type , please ensure your returned type is pure json. – kobe Jul 03 '11 at 01:36
  • You can make this work in 2.0, but you need to have the ASP.NET AJAX Extensions installed (that wasn't bundled until 3.5) and modify your web.config to use it. Some info and a link to the download here, though I think you may also need the HttpModule item in the web.config for ASPX page methods: http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/ – Dave Ward Jul 03 '11 at 04:34

3 Answers3

2

try this it will work .

As your returned type is not pure json or it is thinking thats why it is failing ,

remove datatype:json and give a try , it should work

$.ajax({
                type: 'POST',
                url: '/Search/BasicSearch.aspx/sayHello',
                contentType: "application/json; charset=utf-8",
                data: '{}',
                success: function (msg) {
                    alert(msg.d);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus + '  ' + errorThrown);
                }
            });
kobe
  • 15,671
  • 15
  • 64
  • 91
  • With this approach, the success event actually fires, but msg is the html for the whole page. Again, got to be something with .net 2.0. I am just going to make a JSON based service to house all functions I plan to call via jquery. – user163757 Jul 03 '11 at 12:50
  • I just tried this way on my work computer and it works. not sure if I had a typo at home or what. – user163757 Jul 13 '11 at 16:46
1

After working a lot with this problem, I find out the solution:

You are telling your AJAX call that the response is JSON format, and that is not correct:

contentType: "application/json; charset=utf-8", 
dataType: "json",

.NET web services do not response in JSON (not without configuring them at least). Delete dataType and set contentType to "charset=utf-8".

Perception
  • 79,279
  • 19
  • 185
  • 195
1

Try this:

return '{"": "hello world"}';
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145