简体   繁体   中英

Posting form with Ajax to a different server getting is not allowed by Access-Control-Allow-Origin error

  $this->output->set_header("Access-Control-Allow-Origin: *");

This line seems to work for accessing pages from another server via ajax but when I try to post data to another server I'm still getting an error even though the line above is in my script.

Here is the Ajax I'm using to post the data.

         $.ajax({
            url: "' . base_url('pagehere') . '",
            type: "POST",
            data: "type=pform&ip="+ip+"&host_server="+host_server+"&mediabar="+mediabar+fields,
            success: function(msg) {
                $("#panelform").hide();
                $(".pthankyou").show();
            }
            });

Here is what I have on the page I'm posting to:

        $this->output->set_header("Access-Control-Allow-Origin: *");
        $this->output->set_header("Access-Control-Allow-Methods: POST");

I'm not sure what else I need to make this work.

you have to set your ajax with credentials:

$.ajax({...
xhrFields:{
        withCredentials: true
      },
});

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