You can request and get jsonp ONLY IF the web service you are hitting is set up for cross domain access, so your ajax call has to be right and the webservice has to be right.
ajax call
$.ajax({
type: "GET",
cache: false,
dataType: 'jsonp',
// we are setting base_url at the top, like http://www.MyDomain.com/MyPage.svc/
url: base_url + "GetGfeQuote?strJsonRequestObject=" + JSON.stringify(LoadedGetQuoteObject()),
contentType: "text/plain",
success: function (theJson) {
// I make sure I got json
if (theJson.indexOf('{') > -1 ) {
glb_the_quote = $.parseJSON(theJson);
if (glb_the_quote.errorMessage.length == 0) {
PopulateResultsPage();
} else {
alert('There was an error getting the quote: ' + glb_the_quote.errorMessage);
}
} else {
alert(theJson)
}
},
error: function (req, status, error) {
if(status == "timeout"){
ShowNoInternetConnectionWarning();
} else {
alert("There was an internet error = " + status + ", " + error);
}
},
// this gives the webservice 7 seconds to return
timeout: 7000
});
// end ajax;
Now the webservice: At one point it seemed that I had to have a web config, configured correctly, in the same dir as the webservice code - the .svc file - so that is what I do.
This is all I put in my svc file:
<%@ ServiceHost Language="C#" Factory="System.ServiceModel.Activation.WebServiceHostFactory" Debug="true" Service="gfeWebService.ws.wsGfe" CodeBehind="wsGfe.svc.cs" %>
And the webconfig has to have stuff like the following (note crossDomainScriptAccessEnabled="true" )
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<!-- the names have to be fully qualified. If you get an error that says, I can't find blah blah, you don't have the names right -->
<services>
<service name="gfeWebService.ws.wsGfe">
<endpoint address=""
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP"
contract="gfeWebService.ws.IwsGfe"
behaviorConfiguration="webHttpBehavior"
>
</endpoint>
</service>
</services>
</system.serviceModel>
Tips
put a break point in you js code near the url: line, grab the value that is ending up in url: ... in other words, grab how this resolves
base_url + "GetGfeQuote?strJsonRequestObject=" + JSON.stringify(LoadedGetQuoteObject())
and paste it in the address box of your browser. You get a more meaningful error message that way.
- have Fiddler running while you work on this, and check out what is being sent n received.
HTH