Parsererror error when making an AJAX crossDomain request

Asked

Viewed 496 times

1

I created this page to test a request cross Domain with AJAX:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">
    </script>
</head>
<body>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                url: "/Services/WebServerStatus.asmx/TestConnection",
                type: "POST",
                crossDomain: true,
                dataType: "jsonp",
                data: { key: "querty" },
                success: function (data) {
                    alert('good');
                },
                error: function (xhr, status, error) {
                    alert(status);
                }
            });
        });
    </script>
</body>
</html>

In the script I use dataType: "jsonp" 'cause I read around that he’s needed for that and I didn’t get with just dataType: "json".
With both, "jsonp" or "json", I can stop the process with a break-point at the thresh the service. But with "json" receive the error message "error" and "jsonp" is "parsererror".

How can I fix this?

My Webmethod:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void TestConnection(string key)
{
    var serializer = new JavaScriptSerializer();
    var json = serializer.Serialize(new { 
        OnLine = true, 
        Message = "Requisição efetuada com sucesso!" 
    });
    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.Flush();
    Context.Response.Write(json);
}

EDITION

For Postman, plugin of Google Chrome to make requests, using the option "form-data" to place the request with the post method, I get the following error:

System.Invalidoperationexception: Invalid request format: Multipart/form-data; Boundary=---Webkitformboundarygllkc2yniyzbstbm. in System.Web.Services.Protocols.Httpserverprotocol.Readparameters() in System.Web.Services.Protocols.Webservicehandler.Coreprocessrequest()

But, changing to "x-www-form-urlencoded" I can already perform the request and receive my object json for Postman.

So, what I need to change in my script?

1 answer

1

Postman should handle this issue alone, but I needed to change my Webmethod a little so I could handle the return.

As accepted in this answer question in the Soen, jsonp return is expected so that the json result is encapsulated in a return-handling method.

The comeback must be something like callback({....});.

Soon, I changed my Webmethod to this:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void TestConnection(string key)
{
    var serializer = new JavaScriptSerializer();
    var json = serializer.Serialize(new { 
        OnLine = true, 
        Message = "Requisição efetuada com sucesso!" 
    });
    Context.Response.Clear();
    Context.Response.ContentType = "application/json";    
    Context.Response.Flush();

    var callback = Context.Request.QueryString["callback"];
    if (!string.IsNullOrEmpty(callback))
        Context.Response.Write(string.Format("{0}({1});", callback, json));
    else
        Context.Response.Write(json);
}

There, with that my request was able to handle the return and the process was carried out successfully.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.