Jsonp post cross-origin scheme, jsonppost cross-Origin

Source: Internet
Author: User
Tags php server

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.