parsererror error while executing a crossDomain AJAX request

1

I've created this page to test a cross domain request with AJAX :

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script></head><body><scripttype="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" because I read that it is necessary for this and I could not do it with only dataType: "json" . With both jsonp or " json ", I can stop the process with a break- point to the debugar service. But with " json " I get the error meson " error and " jsonp "is " parsererror .

How can I resolve 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

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

  

System.InvalidOperationException: Invalid request format: multipart / form-data; boundary = ---- WebKitFormBoundarygLlKC2yNIYzbStBM.      at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters ()      at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest ()

But, changing to x-www-form-urlencoded I can already accomplish the request and receive my object json in by Postman .

So what do I need to change in my script?

    
asked by anonymous 16.09.2014 / 14:15

1 answer

1

Postman should handle this issue on its own, but I needed to change my WebMethod a bit so I could handle the return.

As you accept this question in SOen , the return of the jsonp type is expected so that the json result is encapsulated in a method that handles the return.

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

So 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);
}

So, my request was able to handle the return and the process was successful.

    
16.09.2014 / 16:55