我有以下ajax脚本
dataString = 'cipher'; //
var jsonString = JSON.stringify(dataString);
$.ajax({
type: "POST",
url: "tokenize.php",
data: {data : jsonString},
cache: false,
success: function(){
alert("OK");
}
});
returnedvalue = result //I wanted to store the value returned by php in this variable
alert(returnedvalue);
tokenize.php 是
$data = json_decode(stripslashes($_POST['data']));
return $data; //Pass this value as ajaxs response
但我无法得到这个。当我检查控制台时,我收到错误未捕获:结果未定义。
我是查询新手,在谷歌上搜索并完成了此操作。
json 不是必需的,我想做的就是将一个值传递给 php 并处理它,然后将 rssponse 返回给 javascript,以便我可以在 javascript 中使用它
最佳答案
您只是将 string(dataString = 'cipher';
) 传递到 ajax 文件中。不需要 JSON。
To use
echo
for return values from AJAX file.
JS 更新:
dataString = 'cipher'; //
$.ajax({
type: "POST",
url: "tokenize.php",
data: {data : dataString},
cache: false,
success: function(result) { //just add the result as argument in success anonymous function
var returnedvalue = result;
alert(returnedvalue);
}
});
在 PHP 文件中更新:
$data = stripslashes($_POST['data']);
echo $data;
关于javascript - 使用ajax从php到javascript获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22741486/