简体   繁体   中英

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);
          }
      });
$.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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM