简体   繁体   中英

How to get ajax response from cross-domain by posting some data to the controller method.while working at online server

I want to get array data from Codeigniter controller,With ajax request.I am posting some data to the controller for getting related data from database.It works fine locally but when I uploaded my website to online domain and hosting I cant get any response.

My code works fine on local server.But when I Uploaded it to online server my ajax requests not work at all.There is no available response data.below is code works fine at local server

$.ajax({
            type:'POST',
            url:'<?php echo base_url()?>getstate',
            data:'country_id='+country_id,
            success:function(data){
              var dataObj = jQuery.parseJSON(data);
               $(dataObj).each(function(){

                          // Add options
                          $.each(dataObj,function(index,data){
                              alert(data['data_name']);

                          });


                    });
                }
 })

server side code is

 public function getstate()
    {
        $state = array();
        $country_id = $this->input->post('country_id');
        $state = $this->Country_model->getstate($country_id);
        echo json_encode($state);

    }

I have try this

var xhr=new XMLHttpRequest();
      xhr.oppen("GET","http://www.my-domain-name.co/getstate",true);
      xhr.onreadystatechange = function()
      {
          if((xhr.readyState == 4)&&(xhr.status==200))
          {
              var ob=JSON.parse(xhr.responseText);
              alert(ob.name);
              }
        };
        xhr.send();

but does not work

How can I do this.I am new to this situation...

their are 4 possible reason/solution

first please check your CI version if it is old one please check for base_url definition

if site got secure url(https) now .

2nd add crossDomain parameter to jquery ajax function as follow :

 $.ajax({
        type:'POST',
         crossDomain : true,
        url:'<?php echo base_url()?>getstate',
        data:'country_id='+country_id,
        success:function(data){
          var dataObj = jQuery.parseJSON(data);
           $(dataObj).each(function(){

                      // Add options
                      $.each(dataObj,function(index,data){
                          alert(data['data_name']);

                      });


                });
            }
})

check this for more details

3rd its got blocked by the server your are requesting to then it wont be possible until you have the target web site under your control or other access specifires

4th
blocked by your hosting : set headers ( like

  • Access-Control-Allow-Origin ,
  • Content-Security-Policy
  • etc

)for allowing them .

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