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?