简体   繁体   English

AJAX调用跨域问题

[英]AJAX call cross domain issue

I am trying to login to my wordpress site using ajax call, which i asked in previous question . 我正在尝试使用ajax调用登录我的wordpress网站,我在上一个问题中提到过 It should not respond anything back. 它不应该回应任何回应。 But whenever i call this ajax function, Firefox(firebug) is showing 200 OK in red. 但每当我称之为ajax函数时,Firefox(firebug)显示200 OK红色。 This must be a cross domain issue. 这必须是跨域问题。 Tried too many things but no luck so far. 到目前为止尝试了太多但没有运气的东西。 Here is the code: 这是代码:

$.ajax({
         type: "POST",
         url: "http://path-to-wordpress/wp-login.php",
         data:myData
         success: function(data){

         },
        error: function (xhr, ajaxOptions, thrownError) {

        }   
});

Any suggestion will be appreciated. 任何建议将不胜感激。

I assume your JS code is on abc.com and your wordpress site is dev.abc.com . 我假设您的JS代码在abc.com而您的wordpress网站是dev.abc.com

To fix cross domain issue, just add the following code to .htaccess file to your wordpress site ( dev.abc.com ) 要修复跨域问题,只需将以下代码添加到.htaccess文件到您的wordpress站点( dev.abc.com

SetEnvIf Origin "^http(s)?://(.+\.)?(abc\.com)$" origin_is=$0 
Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is

Just replace (abc\\.com) with a proper site name. 只需用适当的站点名称替换(abc\\.com) For eg: (google\\.com) or (yahoo\\.com) 例如: (google\\.com)(yahoo\\.com)

Set the dataType to JSONP , in your ajax call and it will work cross-domain 在ajax调用中将dataType设置为JSONP ,它将跨域工作

For refrence ot ajax call visit jQuery Ajax 对于参考ajax调用访问jQuery Ajax

There is property named crossDomain of $.ajax. 有一个名为crossDomain $ .ajax的属性。 Please set it to true. 请将其设置为true。

You need to add CORS headers in you wp-login.php page. 您需要在wp-login.php页面中添加CORS头

Here's how to do it 这是怎么做的

But given the nature of the page, be extra cautious : don't allow all origins (*) but only your site . 但鉴于页面的性质,要格外小心: 不要允许所有来源(*),只允许你的网站 If you allow all, your users could have their login info stolen. 如果您允许所有人,您的用户可能会盗取他们的登录信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM