Home > Web Front-end > JS Tutorial > body text

Jsonp post cross-domain solution

高洛峰
Release: 2017-01-12 09:59:41
Original
979 people have browsed it

Recently I encountered such a problem in the project. Regarding the cross-domain problem of jsonp, it is possible to pass the value through get, but it is not possible to pass the value through post. So I read a lot of information about this on the Internet, and finally the problem was resolved. Solved, I will take the time to share it with you today.

Instructions:
http://www.t1.com/index.php Server URL
Of course this is my local configuration and needs to be changed to my own corresponding address.

Client code:

<script>
  $(function(){
      
    var url = &#39;http://www.t1.com/index.php&#39;;
    $.ajax({
      type:      &#39;post&#39;,
      url:      url,
      data:      {name:&#39;wangyulu&#39;},
      dataType:    &#39;jsonp&#39;,
      success:function(result){
        console.log(result);
      }
    });
      
  });
 </script>
Copy after login

Server code:

<?php
if($_POST){
  $arr = array(&#39;name&#39;=>$_POST[&#39;name&#39;], &#39;age&#39;=>23);
  echo json_encode($arr);
}
Copy after login

The browser runs as shown below:

Jsonp post 跨域方案

Look Run the results below. Even if the transmission method is set to post, Jquery will automatically switch to get. Does Jsonp really not support post submission? Continue to toss...

There is a change in the arrow mark, don’t worry about it for now. Too much, I have been struggling for so long, let’s put it into the program first, just in case it works

Client change:
Add: crossDomain: true
Modify: dataType: "json"

Added on the server side:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header( 'Access-Control-Max-Age: 1000');

The running results are as follows:

Jsonp post 跨域方案

At this point we found that the problem was finally solved, but we Detailed observation found that the request time is very long. It seems that foreigners do not have an efficient solution.

Finally, what I want to say is that it is really difficult to perfectly support JSONP in POST mode. Let’s do it, let’s stop here, it’s taken a lot of time.

The above content may be related to other technical points. If you are interested, you can study it yourself. The main solution here is Jsonp. If it is not well written, please do not complain, thank you!

The above is the entire content of this article, I hope you all like it.

For more Jsonp post cross-domain solution related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!