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?