0

I want to get data from the webservices through jquery ajax call(cross domain). After fetching data from webservices, i need to show it as a dataTable using php. Can anyone help me regarding this or just give me some sampe examples.

my ajax function is as follows:

$.ajax({
      type: "POST",

      url:"my webservice url",

      //data: json,
      //contentType: "application/json; charset=utf-8",
      crossDomain: true,
      dataType: 'json',
      async:false,

      success: function(data, textStatus, jqXHR)
          {
              alert("Download success");
              alert(data);
          },
          error : function(jqXHR, exception) 
          {
              alert(jqXHR.status);
          }
      });
SuperMykEl
  • 593
  • 2
  • 15
amrita
  • 15
  • 1
  • 5
  • http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Anonymous Jan 28 '13 at 12:38
  • can you express better your need ? are you having trouble with cross domain ajax call or need guidance to show the results in the dataTable ? – Abu Romaïssae Jan 28 '13 at 13:04
  • Atfirst i want to solve my problem with cross domain ajax call. – amrita Jan 28 '13 at 13:14
  • i tried in many ways for cross domain ajax call but i am not able to get the data. It is always giving me error – amrita Jan 28 '13 at 13:23
  • @user2018163 what is the Error ? IS it JAVA SCRIPT Error ?? Please update your Question with that Error. – Fawad Ghafoor Jan 28 '13 at 13:29
  • As sayed by @FawadGhafoor what you need is JSONP, since cross domain requests are not allowed - it's a security flaw. With JSONP you can only do GET requests sending a token and the server response with the same key. – Ragen Dazs Jan 28 '13 at 13:50
  • @FawadGhafoor.... I tried doing with datatype as 'jsonp' but getting json parse error – amrita Jan 28 '13 at 14:54

2 Answers2

1
$.ajax({
   url:"yourPageName.php",
   dataType: 'jsonp', // N.B! JSONP   It is lower Case OK?
   success:function(json){
     // json (an  Array)
     alert("Success");
 },
 error:function(){
     alert("Error");
 },

});

For more info please visit here http://api.jquery.com/jQuery.ajax/

Fawad Ghafoor
  • 5,029
  • 5
  • 38
  • 53
  • i tried with datatype as 'jsonp'.The jqXHR.status is 200 but it is still entering the error function as it is throwing now json parse error – amrita Jan 28 '13 at 14:44
0

Jsonp is better way to do it. But if you really do with json you can add

header("Access-Control-Allow-Origin: *");

to your php code. This way your server will response any request and domain. You can customize "*" to accept domain. But be aware this will cause security issue.

Brend
  • 194
  • 6