简体   繁体   English

跨域 Ajax 的动态脚本标记不起作用

[英]Dynamic script tag for cross-domain Ajax not working

I'm trying to use the dynamic script tag for cross-domain ajax, since I can't use PHP on the website where it's needed (we're using a CMS, so we can use HTML/Javascript, but no PHP).我正在尝试将动态脚本标签用于跨域 ajax,因为我无法在需要它的网站上使用 PHP(我们使用的是 CMS,因此我们可以使用 HTML/Javascript,但没有 PHP)。

The thing is, both scripts work well (javascript and php) on their own, meaning the script tag is well added, and the PHP scripts, when launched on its own, works as needed.问题是,这两个脚本(javascript 和 php)都运行良好(javascript 和 php),这意味着脚本标签被很好地添加,并且 PHP 脚本在单独启动时可以根据需要运行。

But it looks like the GET request is not processed when the script tag is added (using.appendChild).但是在添加脚本标签(using.appendChild)时,似乎没有处理 GET 请求。

Is there a way to make it work?有没有办法让它工作?

Here is the code, simplified:这是代码,简化:

PHP: PHP:

$email = $_GET['email'];
$source = $_GET['source'];
echo 'callback({"value": "true"});';
// sending the email through php

Javascript: Javascript:

function dlPj() {
  // Prompt the user for his email
  var email = prompt('Enter your email', 'your email');
  // If the script tag already exists, delete it
  if (document.getElementById('script-pj') != null) {
    var script_pj = document.getElementById('script-pj');
    document.getElementsByTagName("head")[0].removeChild(script_pj);
  }
  // Create the script tag to call the PHP script
  var s = document.createElement('script');
  s.type = 'txt/javascript';
  s.id = 'script-pj';

  s.src = 'http://www.something.com/bel/pj.php?';
  s.src += 'email=' + email;
  s.src += '&source=' + document.location.href + '?';

  document.getElementsByTagName("head")[0].appendChild(s);
}

function callback(value) {
  if (value == null) {
    alert('error');
  }
  else {
    if (value.value == "true") {
      alert('working');
    }
    else {
      alert('not working');
    }
  }
}

Help greatly appreciated.非常感谢帮助。

Edit: problem solved.编辑:问题解决了。 The problem was the following: I added an onclick event on an A tag, so that I could get some information about a user before he downloads an attached file.问题如下:我在 A 标签上添加了一个 onclick 事件,这样我就可以在用户下载附件之前获得一些关于用户的信息。 So I left it at return true;, so that he could download the file after filling in the form.所以我把它留在了return true;,这样他就可以在填写表格后下载文件。 Changing the onclick event to "dlPj(); return false;"将 onclick 事件更改为“dlPj(); return false;” solves the problem.解决问题。

Try this, it works in chrome:试试这个,它适用于 chrome:

  var email = prompt('Enter your email', 'email@company.com');
  // If the script tag already exists, delete it
  if (document.getElementById('script-pj') != null) {
    var script_pj = document.getElementById('script-pj');
    document.getElementsByTagName("head")[0].removeChild(script_pj);
  }
  // Create the script tag to call the PHP script
  var s = document.createElement('SCRIPT');
  s.id = 'script-pj';
  s.src = 'http://www.something.com/bel/pj.php?' +
                'email=' + encodeURIComponent(email) + 
                '&source=' + encodeURIComponent(document.location.href);

  document.getElementsByTagName("head")[0].appendChild(s);

Alright, so that this follows correctly stackoverflow's workflow, there is the answer.好的,这样就可以正确地遵循 stackoverflow 的工作流程,这就是答案。

The problem was the following: I added an onclick event on an A tag, so that I could get some information about a user before he downloads an attached file.问题如下:我在 A 标签上添加了一个 onclick 事件,这样我就可以在用户下载附件之前获得一些关于用户的信息。 So I left it at return true;, so that he could download the file after filling in the form.所以我把它留在了return true;,这样他就可以在填写表格后下载文件。 Changing the onclick event to "dlPj(); return false;"将 onclick 事件更改为“dlPj(); return false;” solves the problem.解决问题。

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

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