1

I have created a WCF ajax enabled web service named "Service1.svc" "I have to call this Service In another app's using Jquery." I have created on method in it :

    [OperationContract]             
    public string GetMarkup()
    {   
       string data = "<div>My HTML markup text here</div>";
       return data;
    }

Now I have created jquery script in my second application's html page :

var markup = "";
$.ajax({
    type: "POST",
    url: "http://localhost:1676/MyWCFService.svc/GetMarkup",
    contentType: "application/json",
    data: "{}",
    dataType: "json",
    success: callback,        
    error: function (textStatus) {
        alert("ERROR");
    }
});
function callback(result) {
    alert("Inside Callback");
    markup = result.d;
    $("#divMyMarkup").html(markup);
    alert(markup);
}

NOW, My Problem is that Whenever I execute this page in IE its working fine. But In Firefox its not working. It giving alert Error Message which defined in error: function (textStatus) {alert("ERROR");} in above ajax call.

I tried this functionality using $.get(), $("#divMyMarkup").load(serviceUrl, callback). I also tried this by changing the datatype as json, jsonp, html . Still I am not getting the exact solution.

Any Expert here?

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
kapil k
  • 11
  • 1
  • 2
  • Why not alert the textstatus param in the error callback rather than a string. The contents of textstatus may help you, also have you used fiddler to see if the xhr call is actually made? Does it respond with a status of 200? Is your page served with the same url and port? Are you falling foul of the same origin policy? – redsquare Aug 05 '11 at 13:13
  • when the get request it giving "405 Method Not Allowed localhost:1676" in firefox and error : undefined in alert message. – kapil k Aug 05 '11 at 13:21
  • Sounds like you're making a cross-domain call to me. You said 'my second application' is where you're making the ajax call. Just changing the data type to `jsonp` isn't going to make it work. Are you making a cross-domain call here? – David Hoerster Aug 05 '11 at 13:46
  • yes david.. I tried with jsonp... One more interesting thing is that when I keep the contenttype : "text/plain", the service hit from IE and Firefox both. But in firefox response is empty and get service is 200 OK. – kapil k Aug 05 '11 at 13:47
  • You cannot do this cross domain. You can use jsonp but you will need to change your service to return the json wrapped in the callback function see http://jasonkelly.net/2009/05/using-jquery-jsonp-for-cross-domain-ajax-with-wcf-services/ – redsquare Aug 05 '11 at 14:27

1 Answers1

3

In another app's using Jquery

In my experience, IE won't respect the cross-domain policy and let you do the call, not a reference...

The only way to find out is to have your html page/JQuery script calling your WCF service from http://localhost:1676/ICallWcfServicesWithJQuery.html on Firefox.

Possible solutions:

Test on multiple browsers, add 1oz of gin, a can of tonic and you'll be good!

Community
  • 1
  • 1
maxbeaudoin
  • 6,546
  • 5
  • 38
  • 53