Reputation: 2118
I have a C# web application in which I'm calling a web service using ajax. The ajax code looks like as below
$.ajax({
type: 'GET',
url: "WebServices/testwebService.asmx/Methodname",
contentType: "application/json; charset=utf-8",
data: { value: "string" },
dataType: 'xml',
success: function (data) {
alert(data);
},
error: function (data) {
alert("In error");
}
});
});
The service 'WebServices/testwebService.asmx/Methodname' returns a list of strings (or can be a string)
Here if Im giving type: 'GET' and dataType: 'json' it is redirecting to the error part.
If it is given as dataType 'json' or 'string' with type: 'POST', or dataType 'xml' or 'string' with type: 'GET' it is redirecting to the success part. And 'xml' dataType with type: 'POST' redirecting to the error part.
I am very confused with the dataType concept in ajax.
When I checked in Firebug, the response data always be in xml format.
Upvotes: 1
Views: 1323
Reputation: 1964
ASMX SOAP web service invoke the method using HTTP-POST Protocol.So ideally the "type" parameter in the ajax call should have the value 'POST'.(check the MSDN documentation)
Regarding the data type,the response data purely depends on what Content Type & Data Type you have mentioned in the ajax call.Depending on the data type parameter JQuery processes the response from the server,which is always XML for a SOAP web service.
If you made data type parameter value as 'xml' you will get an XML document in the success callback function.If you made it JSON,you will be getting JSON Object in the success callback function.
Upvotes: 1
Reputation:
if you are using the json dataType, server should return a json. Please go through this link, if you have any doubt regarding dataTypes.
Upvotes: 1