amrita
amrita

Reputation: 15

cross domain ajax call + php

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);
          }
      });

Upvotes: 0

Views: 1570

Answers (2)

Brend
Brend

Reputation: 196

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.

Upvotes: 0

Fawad Ghafoor
Fawad Ghafoor

Reputation: 6207

$.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/

Upvotes: 1

Related Questions