I'm trying to get a JSON from a webservice with javascript $.Ajax call.
<script type="text/javascript">
$(function () {
$("#" + "@Model.BidObjectId").submit(function () {
alert("Test");
var param = { 'id': "@Model.BidObjectId" };
$.ajax({
url: "http://localhost:11523/Service1.svc/GetBidObject",
dataType: "jsonp",
contentType: "application/json;charset=utf-8",
type: "GET",
data: JSON.stringify(param),
success: function (msg) {
alert("success");
if (msg != null) {
return msg.URL;
}
},
error: function (msg2) {
alert(msg2);
}
});
return false;
});
});
</script>
I always go in the error scenario with a parsererror
status: 200 statusCode: function ( map ) { statusText: "parsererror"
I already read an explaination here but I cannot use JSON because this create some OPTION call. I have tried changing the POST to a GET, returning the data in a few different ways (creating classes, etc.) but I cant seem to figure out what the problem is. Only the solution with JSONP seems to be agreed to do a correct GET or POST. other solutions dont' even find my webservice.
Here is the code of my webservice:
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetBidObject?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string[] GetBidObject(string id);
with
public string[] GetBidObject(string id)
{
BidObject bidobject = new BidObject() { BidObjectId = 1, Title = "callback" };
JavaScriptSerializer ser = new JavaScriptSerializer();
string result = ser.Serialize(bidobject);
List<string> listResult = new List<string>();
listResult.Add(result);
return listResult.ToArray();
}
I don't work with ASP.NET but with Razor.
[EDIT]
If I change jsonp by json in Fiddler I can read my call is OPTIONS http://localhost:11523/Service1.svc/GetBidObject?{%22id%22:%220%22} HTTP/1.1
with chrome. In IE Fiddler detect nothing. The ajax call is never done... I really don't understand.