繁体   English   中英

php变量与javascript的串联

[英]Concatenation of php variable with javascript

var settings = {
  "async": true,
  "crossDomain": true,
  "url": 'https://example.com/something.aspx?i='<? echo urlencode($_GET['id']); ?>,
  "method": "GET",
  "headers": {
    "cache-control": "no-cache",
  }
}

这样行不通,我认为连接是错误的。 尝试了几种方法仍然无效。

您需要将数据放入 JavaScript字符串文字中。 '移到要输出的多余数据之后。

您只是单引号有错误的一面。

不要忘记您正在输出到HTML,因此您不必将PHP变量连接到JavaScript变量。

var settings = {
  "async": true,
  "crossDomain": true,
  "url": 'https://example.com/something.aspx?i=<?php echo urlencode($_GET['id']); ?>',
  "method": "GET",
  "headers": {
    "cache-control": "no-cache",
  }
}

也许您可以反过来尝试。 喜欢

<?php
  echo 'var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://example.com/something.aspx?i='.urlencode($_GET['id']).'",
    "method": "GET",
    "headers": {
      "cache-control": "no-cache",
    }
  }'; 
?>

请注意,我对url属性中的'和'所做的更改。

暂无
暂无

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

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