0

I am using JSONP in order to make a AJAX Cross domain call as following:

 $(document).ready(function () {
          $("#btnWCFREST").click(function () {
          $.ajax({
                  type: "GET",
                  url: "http://localhost:1415/MyService.svc/rh/data?id=4&callback=mycallback",
                  processData: false,
                  dataType: 'jsonp',
                  jsonpCallback: 'mycallback',
                  jsonp: 'callback'


              });


          });
      });

      function mycallback(data) {
          alert(data);
      };

I am not getting any response. Could anybody help me in order to resolve the issue? What is wrong with the call?

I have changed the call as following still no luck:

 $(document).ready(function () {
          $("#btnWCFREST").click(function () {
                 var url = "http://localhost:1415/MyService.svc/rh/data?id=4";
                  $.getJSON(url + "?callback=?", null, function(data) {
                    alert(data);
                });
         });
      });
user1672097
  • 361
  • 1
  • 4
  • 12
  • have a look at http://bloggingabout.net/blogs/adelkhalil/archive/2009/08/14/cross-domain-jsonp-with-jquery-call-step-by-step-guide.aspx – Dakait Jan 01 '13 at 07:10

2 Answers2

1

Jquery's ajax documentation says: Adds an extra "?callback=?" to the end of your URL to specify the callback. Your url doesn't meet that requirement, so it's treated as a JSON (instead of JSONP) request, which will be subject to the limit of same origin policy. You may refer to a working example on Stackoverflow: jsonp with jquery. Besides, you should make sure that the cross-domain server supports JSONP.

Community
  • 1
  • 1
Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
  • Thanks for the answer. According to the working example provided in the above line, I have changed the call as following but no luck: $(document).ready(function () { $("#btnWCFREST").click(function () { var url = "http://localhost:1415/MyService.svc/rh/data?id=4"; $.getJSON(url + "?callback=?", null, function(data) { alert(data); }); }); }); – user1672097 Jan 01 '13 at 07:45
  • Does the service provided by `MyService.svc` support JSONP? It's supposed to return the feedback whose payload is something like `mycallback(requested JSON data)`. – Hui Zheng Jan 01 '13 at 07:50
  • Its a WCF REST service and returning JSON as data. Do I need to perform anything addtional at service end? – user1672097 Jan 01 '13 at 07:54
  • You shouldn't return plain JSON as feedback data. Instead, return something like `mycallback({"Name": "Foo", "Id": 1234, "Rank": 7})`. Please refer to this: http://en.wikipedia.org/wiki/JSONP. – Hui Zheng Jan 01 '13 at 07:59
0

Try adding crossDomain : true in your Ajax settings :

       $.ajax({
              type: "GET",
              url: "http://localhost:1415/MyService.svc/rh/data?id=4&callback=mycallback",
              processData: false,
              dataType: 'jsonp',
              jsonpCallback: 'mycallback',
              jsonp: 'callback',
              crossDomain: true
       });
wakooka
  • 1,398
  • 10
  • 15