Ajax requests a method userInfo under the domain name to obtain user information. However, the userInfo method for obtaining user information uses the single sign-on system. Therefore, the process of obtaining information exists across domains, in this way, an error will be reported for ajax requests. How can this problem be solved?
Ajax requests a method userInfo under the domain name to obtain user information. However, the userInfo method for obtaining user information uses the single sign-on system. Therefore, the process of obtaining information exists across domains, in this way, an error will be reported for ajax requests. How can this problem be solved?
Reply content:
Ajax requests a method userInfo under the domain name to obtain user information. However, the userInfo method for obtaining user information uses the single sign-on system. Therefore, the process of obtaining information exists across domains, in this way, an error will be reported for ajax requests. How can this problem be solved?
Methods
1-use the local php to get it through curl. In this way, ajax will not cross-origin as long as it requests the local php. The PHP-side curl has no cross-origin restrictions.
2-The host provides js scripts, and the associated system references this js. Calling the declared Method for ajax requests does not belong to cross-origin.
Usually I use method 1 and method 2, which requires the cooperation of the developers of the host system, which is too troublesome.
Make a proxy at the php layer so that php can request the cross-origin interface, and then php will return the data to the page.
Jsonp cannot be used for this interface. It is neither implemented nor secure. User information cannot be leaked, because it is userinfo, so you must pay attention to security issues.
During cross-origin, a callback function must be added to the address parameter to process the returned data in js. Here is a cross-origin post. For more information, see. Https://github.com/limeng0403/libs/blob/master/EOF/getJSON%E5%9C%A8PHP%E7%8E%AF%E5%A2%83%E4%B8%8B%E5% AE %9E%E7%8E%B0%E8%B7%A8%E5%9F%9F%E6%95%B0%E6%8D% AE %E5%8A%A0%E8%BD%BD.md
Use ajax jsonp
-
PureGETRequests can be usedjsonp
-
SupportedCORSBrowser, availableCORS
Request data steps
Send request ---> Server Response Return ---> browser accept response ---> determine whether the response is in the same domain
If yes, return the response data to ajax. If not, cross-origin requests are forbidden.
In fact, the server can add
Header ("Access-Control-Allow-Origin: allowed domain ");
Header ("Access-Control-Allow-Methods: method allowed for requests ");
Header ("Access-Control-Allow-Headers: X-PINGOTHER ");
Tell the browser to return the response value to this request
Reference: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS
For cross-origin, refer to the following article.
Http://www.cnblogs.com/duanhuajian/p/4211616.html
Header ('content-type: application: json; charset = utf8 ');
Header ('access-Control-Allow-Origin :*');
Header ('access-Control-Allow-Methods: Post ');
Header ('access-Control-Allow-Headers: x-requested-with, content-type ');
Add this in your php file
JSONP requests can be used
Three methods I 've just sorted out to address cross-origin requests: https://segmentfault.com/a/1190000004539815
PostMessage (); onmessage.
The best thing is to use the php curl proxy in the past, with a higher level of control.
The second is to modify the server interface file and open the cross-origin restriction so that ajax can be called.
Third, we recommend that you useJsonp.
The specific single sign-on system is not implemented. The strange thing is that if I access userInfo directly through a browser, the user information can be obtained, ajax then requests the userInfo method to return the user information normally. It is very strange, so I used a very stupid method, that is, using an iframe at the bottom of the page, iframe loading should be an http request, and it will be normal! However, the reason why a cross-domain error is reported during direct access is still not found. Thank you for your answers! @ IncNick @ dameng @ Zhang yatao @ and so on ......