How to solve the Cross-domain problem of Ajax
Today, the editor will share with you the relevant knowledge points about how to solve the cross-domain problems of Ajax. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Cross-domain problem
As shown in the figure, this is a local php file (no framework) requested through the jquery encapsulated ajax, and the console prompts that the CORS policy has blocked the "null" from the source: the "access control allowed source" header does not exist on the requested resource.
Solution
Set the header
Header ("Access-Control-Allow-Origin: *"); allow all domain name requests
Header ("Access-Control-Allow-Origin: http://127.0.0.1"); allows a domain name request
Request to enter the error question successfully
This is mostly because you are required to return data in json format, but the actual data returned is not correct json data.
Var data = {parent:1} $.ajax ({type: "POST", url: "http://127.0.0.1", dataType:'json', success: function (response,index,obj) {console.log (obj.done (function () {/ / callback alert (" $.get succeeded ");})); console.log (index) / / log success console.log ("success", response); / / data returned from a successful request}, error (res) {console.log ("failure", res);}})
The first letter of Ajax is the first letter of asynchronous, which means that all operations are parallel and the order in which they are completed is unrelated. The async parameter of $.ajax () is always set to true, which indicates that other code can still execute after the request starts. Setting this option to false is strongly discouraged, which means that all requests are no longer asynchronous, which can cause browsers to lock up.
These are all the contents of the article "how to solve the Cross-domain problem of Ajax". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.