Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Ajax响应中返回Javascript变量_Php_Ajax_Variables_Return_Response - Fatal编程技术网

Php 在Ajax响应中返回Javascript变量

Php 在Ajax响应中返回Javascript变量,php,ajax,variables,return,response,Php,Ajax,Variables,Return,Response,如何在此处使用eval()函数 ajax.js ajax.php <?php exit("'first string is '+x+' second one is'+y");?> 但上面写着“x是不确定的” 注意:我需要的解决方案不需要从ajax传递x和y作为数据 $.ajax({ method: "post", url:"ajax.php", data: {x: "helloworld", y:"another string"}

如何在此处使用eval()函数
ajax.js

ajax.php

<?php exit("'first string is '+x+' second one is'+y");?>
但上面写着“x是不确定的”

注意:我需要的解决方案不需要从ajax传递x和y作为数据

  $.ajax({
        method: "post",
        url:"ajax.php",
        data: {x: "helloworld", y:"another string"},
        success:function(data){
            console.log(data);
        }
    });
ajax.php:

echo "First string: ".$_POST['x'].", second string: ".$_POST["y"];

您是否正在尝试创建一个echo脚本,通过AJAX向PHP脚本发送一个变量,然后PHP脚本返回该值

如果是这样,您需要为AJAX请求在
数据
设置中提供
x

var x="helloworld";
$.ajax({
    url:"ajax.php",
    data: {
        x: x
    }
    success:function(data){
        console.log(data);
    }
});
然后在PHP脚本中,需要从
POST
数组中检索值,并将其作为响应输出:

<?php
exit($_POST['x']);
和PHP

<?php
$x = $_POST['x'];
$y = $_POST['y'];
exit("First string is $x, second string is $y");

Javascript可以使用
eval()
函数解析从
PHP
获取的字符串。对于一个simpel示例,我在下一个示例中放弃了ajax调用以获取请求,但您明白了。有一个有效的例子

<script>
var x="hello world";
var y="anyother string";

//normally you would get this from ajax
var str = "'first string is '+x+' second one is'+y";

//complete will contain the full string after eval.
var complete = '';
eval('complete=' + str);


alert(complete); //first string is hello world second one isanyother string
</script>

var x=“你好世界”;
var y=“任何其他字符串”;
//通常情况下,您会从ajax获得此功能
var str=“'第一个字符串是'+x+'第二个字符串是'+y'”;
//complete将在eval后包含完整字符串。
var complete='';
评估('complete='+str);
警报(完成)//第一个字符串是hello world第二个字符串是任何其他字符串
现在在您的例子中,当您使用AJAX时,它将变得

<script>
var x="hello world";
var y="anyother string";

$.ajax({
  url:"ajax.php",
  success:function(data){
    //complete will contain the full string after eval.
    var complete = '';
    eval('complete=' + data);
    console.log(complete);
  }
});
</script>

var x=“你好世界”;
var y=“任何其他字符串”;
$.ajax({
url:“ajax.php”,
成功:功能(数据){
//complete将在eval后包含完整字符串。
var complete='';
评估(“完成=”+数据);
控制台日志(完整);
}
});
以上是你问题的实际答案,我仍然反对。看

要想在没有eval的情况下得到这样的东西,你可以使用

<script>
var x = "hello world";
var y = "anyother string";

//normally you would get this from ajax
var str = "first string is {x} second one is {y}";

//complete will contain the full string after eval.
var complete = str.replace('{x}', x).replace('{y}', y);

alert(complete); //first string is hello world second one isanyother string
</script>

var x=“你好世界”;
var y=“任何其他字符串”;
//通常情况下,您会从ajax获得此功能
var str=“第一个字符串是{x},第二个字符串是{y}”;
//complete将在eval后包含完整字符串。
var complete=str.replace('{x}',x).replace('{y}',y);
警报(完成)//第一个字符串是hello world第二个字符串是任何其他字符串

为什么不直接访问ajax响应中的“x”变量?比如console.log(x);在我的例子中,数据是一个很长的HTML字符串,其中包含很多javascript中的变量。现在你的问题已经不可理解了。谢谢,我刚刚修改了我的问题。我已经修改了我的问题,请复习。我想,从我向你展示的内容来看,你至少可以尝试找出如何添加第二个变量。我不是你的代码猴子。我知道,但我不能像那样传递变量。使用eval函数是一件很简单的事情,您不需要使用
eval
来实现这一点。你能更新你的问题,包括你试图解决这两个变量吗?我使用$.globalEval(数据),但它通过错误“x是未定义的”我修改了我的问题,请审查你应得的+50,stackoverflow允许我在未来16小时释放+50慢慢来,我不着急:)很高兴我能帮上忙
<script>
var x="hello world";
var y="anyother string";

//normally you would get this from ajax
var str = "'first string is '+x+' second one is'+y";

//complete will contain the full string after eval.
var complete = '';
eval('complete=' + str);


alert(complete); //first string is hello world second one isanyother string
</script>
<script>
var x="hello world";
var y="anyother string";

$.ajax({
  url:"ajax.php",
  success:function(data){
    //complete will contain the full string after eval.
    var complete = '';
    eval('complete=' + data);
    console.log(complete);
  }
});
</script>
<script>
var x = "hello world";
var y = "anyother string";

//normally you would get this from ajax
var str = "first string is {x} second one is {y}";

//complete will contain the full string after eval.
var complete = str.replace('{x}', x).replace('{y}', y);

alert(complete); //first string is hello world second one isanyother string
</script>