javascript - Why can't $.post() in jquery submit data across domains?
習慣沉默
習慣沉默 2017-05-16 13:36:50
0
3
636

As the title states, why can the ajax method encapsulated in jquery use jsonp to submit data in the get method, but directly use the post method to submit data across domains? What is the root cause?

習慣沉默
習慣沉默

reply all(3)
世界只因有你

jquery’s own functions are not supported, but you can implement cross-domain posts based on jquery.

jquery itself only supports jsonp cross-domain, but the principle of jsonp limits it to only support get.

If you want to implement native jquery cross-domain, add Access-Control-Allow-Origin in the head of your request address, and set the value to the domain name (or *) that calls the API

仅有的幸福

Cross-domain is generally divided into two types:

  1. jsonp cross domain. jsonp originally uses the get file method to bypass cross-domain checks, so it does not support post. There are also third-party libraries that use get to simulate post requests.

  2. COR cross domain. COR cross-domain needs to modify the server-side Access-Control-Allow-Origin response. Generally, the client will send two post requests. The first time is of type option. After the server responds with permission, the second time will send a real request with data.
    If the respondent’s conditions permit, try to use the second cross-domain method, which can directly support post.

If the server configuration cannot be modified, the respondent can look for a third-party library to see if jsonp can be used to simulate post requests.

阿神

Note that it is not that jquery does not support it, but that the browser intercepts the response and gives you an error when it does not allow cross-domain headers. After jquery receives this error, it will report a cross-domain error to you.
You can grab a packet and take a look (be careful not to use the browser's f12 to capture the packet). In fact, the response from the server is there.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template