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

发布于 2024-11-25 10:55:37 字数 1547 浏览 0 评论 0原文

我正在尝试使用动态脚本标签进行跨域ajax,因为我无法在需要它的网站上使用PHP(我们使用的是CMS,所以我们可以使用HTML/Javascript,但不能使用PHP)。

问题是,两个脚本(javascript 和 php)都可以单独运行良好,这意味着脚本标记被很好地添加,并且 PHP 脚本在单独启动时可以根据需要运行。

但当添加脚本标签(使用 .appendChild)时,GET 请求似乎没有被处理。

有办法让它发挥作用吗?

这是简化的代码:

PHP :

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

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');
    }
  }
}

帮助非常感谢。

编辑:问题已解决。问题如下:我在 A 标记上添加了 onclick 事件,以便我可以在用户下载附件之前获取有关用户的一些信息。所以我把它留在return true;,这样他就可以在填写表格后下载文件。将 onclick 事件更改为“dlPj(); return false;”解决了问题。

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).

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.

But it looks like the GET request is not processed when the script tag is added (using .appendChild).

Is there a way to make it work?

Here is the code, simplified :

PHP :

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

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. So I left it at return true;, so that he could download the file after filling in the form. Changing the onclick event to "dlPj(); return false;" solves the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2024-12-02 10:55:37

试试这个,它在 Chrome 中有效:

  var email = prompt('Enter your email', '[email protected]');
  // 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);

Try this, it works in chrome:

  var email = prompt('Enter your email', '[email protected]');
  // 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);
痕至 2024-12-02 10:55:37

好吧,为了正确遵循 stackoverflow 的工作流程,答案就出来了。

问题如下:我在 A 标签上添加了一个 onclick 事件,以便我可以在用户下载附件之前获取有关用户的一些信息。所以我把它留在return true;,这样他就可以在填写表格后下载文件。将 onclick 事件更改为“dlPj(); return false;”解决了问题。

Alright, so that this follows correctly stackoverflow's workflow, there is the answer.

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. So I left it at return true;, so that he could download the file after filling in the form. Changing the onclick event to "dlPj(); return false;" solves the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文