简体   繁体   English

无法将PHP变量传递给jQuery

[英]Having trouble passing PHP variables to jQuery

I'm completely new to jQuery and trying to pass some PHP variables to Jquery. 我对jQuery完全陌生,并尝试将一些PHP变量传递给Jquery。 I'm using CORS to fetch JavaScript and PHP files from my webserver to my blog. 我正在使用CORS从Web服务器获取JavaScript和PHP文件到博客。

On my blog I want jQuery to GET a PHP file and echo the redirect header variables. 在我的博客上,我希望jQuery获取一个PHP文件并回显重定向头变量。 Then I need JavaScript to get those variables and use them to make client side redirects. 然后,我需要JavaScript来获取这些变量,并使用它们进行客户端重定向。 But I'm not sure how to do that, I've tried all day without success. 但是我不确定该怎么做,我整天都尝试不成功。

The PHP file on my server looks like this: 我服务器上的PHP文件如下所示:

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "US") {
echo ('Location: http://www.adomain.com');
}
else if ($var_country_code == "CA") {
echo ('Location: http://www.adomain.com');
}
else if ($var_country_code == "UK") {
echo ('Location: http://www.adomain.com');
}
else if ($var_country_code == "AU") {
echo ('Location: hhttp://www.adomain.com');
}
else if ($var_country_code == "NZ") {
echo ('Location: http://www.adomain.com/');
}
else if ($var_country_code == "ZA") {
echo ('Location: http://www.adomain.com');
}
else {
echo ('Location: http://www.adomain.com');
}
?>

My jQuery requests is as following: 我的jQuery请求如下:

function loadDoc() {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
    if (ajax.readyState == 4 && ajax.status == 200) {
      "US" == x && US436(),
      "CA" == x && CA(),
      "UK" == x && UK(),
      "AU" == x && AU(),
      "NZ" == x && NZ(),
      "ZA" == x && ZA()
    }
};
ajax.open("GET", "//mydomain.com/scripts/redirect.php", true);
ajax.send();
}

loadDoc()

Oke so here is the code that I 'thought' should work. 好的,这是我“认为”应该工作的代码。 The jQuery script is making a request to my server to GET that redirect.php script. jQuery脚本正在向我的服务器发出请求以获取该redirect.php脚本。 I then get the variables and convert them to functions. 然后,我获取变量并将其转换为函数。 (right???) (对???)

I have another JavaScript file that's creating the functions to make the client side redirect which looks something like this: 我还有另一个JavaScript文件,该文件正在创建使客户端重定向的功能,如下所示:

function US() {
window.location.replace("http://adomain.com");
}

Okay, that's it. 好的,就是这样。 Let's hope I didn't forgot something here. 希望我没有在这里忘记什么。 But this is all the code I'm having right now. 但这就是我现在拥有的所有代码。 Like I said I'm trying really hard to wrap my head around this CORS since I never coded with jQuery before. 就像我说的那样,由于我以前从未使用jQuery进行编码,因此我非常努力地将头放在该CORS上。

If anyone is able to tell me what the problem is that would be very much appreciated! 如果有人能够告诉我问题是什么,将不胜感激! Thanks in advance. 提前致谢。

You're not using jQuery, you're using AJAX . 您没有使用jQuery,而是使用AJAX They are different in principle. 它们在原理上是不同的。 One is a JavaScript library and the other is a method of sending and retrieving data from the server. 一个是JavaScript库,另一个是从服务器发送和检索数据的方法。

The issue with your code is that you're trying to set header variables on the server and send them back to the client, in the hopes that it will redirect, but it won't. 代码的问题在于,您试图在服务器上设置标头变量,然后将其发送回客户端,希望它可以重定向,但不会。 Here's what we need to do. 这就是我们需要做的。

Return back the country code to the Browser from your server, like so: 将国家/地区代码从服务器返回到浏览器,如下所示:

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
return $geoplugin->countryCode;

Now in your javascript onreadystatechange callback, we can receive this value and make our comparison: 现在在您的javascript onreadystatechange回调中,我们可以接收到该值并进行比较:

ajax.onreadystatechange = function() {
   if (ajax.readyState == 4 && ajax.status == 200) {
       var country_code = ajax.responseText;

       switch(country_code) {
           case 'US' :
               US();
               break;
           case 'UK' :
               UK();
               break;
           //so on and so forth

       }
   }
}

Hopefully this helps clear things up. 希望这有助于清理问题。

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

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