![](/img/trans.png)
[英]jQuery $.ajax request of dataType json will not retrieve data from PHP script
[英]Getting response from Using dataType script with jQuery on ajax request
不知道这是否应该工作,但是从带有响应的jquery ajax请求中我想评估传递回的脚本。 这里举个例子。
$.ajax({
url: some/path,
dataType: 'script'
});
然后,根据我的回答,我使用php吐出了javascript。
<?php
header("content-type: application/javascript");
$str = <<<EOF
alert('help');
EOF;
echo $str;
如果我是正确的,则在执行请求后应该会弹出警报。
我可以从响应标头中看到它是正确的:
Accept:application/json, text/javascript, */*; q=0.01
响应是:
alert('help');
但是没有警报框被触发。 我是否缺少某些东西,或者无法通过这种方式完成?
希望你能指教。
TY
您应该在$ .ajax中调用成功函数
并执行响应
page1.php中
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
type="text/javascript"></script>
<script>
$.ajax({
url: "page2.php",
dataType: 'script',
success: function(resp) {
resp; //this is your response which is contains alert('help')
}
});
</script>
使page2.php
<?php
header("content-type: application/javascript");
$str = <<<EOF
alert('help');
EOF;
echo $str;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.