Jsonp post cross-origin scheme, jsonppost cross-Origin
I recently encountered such a problem in the project. For the cross-domain issue of jsonp, it is acceptable to pass the value through get, but the transfer of the value through post cannot survive. So I read a lot of information about this on the Internet, the final problem can be solved. I will share it with you today.
Note:
Http://www.t1.com/index.php server URL
Of course, this is my local configuration. You need to change it to your own address.
Client code:
<script> $(function(){ var url = 'http://www.t1.com/index.php'; $.ajax({ type: 'post', url: url, data: {name:'wangyulu'}, dataType: 'jsonp', success:function(result){ console.log(result); } }); }); </script>
Server code:
<?phpif($_POST){ $arr = array('name'=>$_POST['name'], 'age'=>23); echo json_encode($arr);}
The browser runs as follows:
Check the running result. Even if the transmission mode is set to post, Jquery will be automatically converted to get. Does Jsonp really not support post submission? continue to submit the results...
Finally, I found some information about this in stackoverflow. It seems that foreigners have encountered this problem. I will post the address to you.
Http://stackoverflow.com/questions/3860111/how-to-make-a-jsonp-post-request-that-specifies-contenttype-with-jquery
Key points include:
We can see that there is a change in the arrow flag, no matter how much it is, after a long time, it should be put in the program first. What if it is OK?
Client changes:
Add: crossDomain: true
Modify: dataType: "json"
Add a server:
Header ('access-Control-Allow-Origin :*');
Header ('access-Control-Allow-Methods: Post ');
Header ('access-Control-Max-Age: 100 ');
The running result is as follows:
Here we found that the problem was finally solved, but we observed in detail that the request time was a long time. It seems that there was no efficient solution for foreigners,
Finally, what I want to say is that it is really difficult for JSONP to perfectly support the POST method. Let's just get there and it takes a lot of time.
The above content may be associated with other technical points. If you are interested, you can study it. Here, we mainly solve the problem of Jsonp, which is not easy to write. Thank you!
The above is all the content of this article. I hope you will like it.