javascript - 使用ajax从php到javascript获取值

标签 javascript php jquery ajax

我有以下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/

相关文章:

javascript - API 耗时太长,映射函数在数据加载之前触发

javascript - 从服务器获得响应后关闭 Controller 中的 Bootstrap 模式

javascript - 为什么默认参数值不起作用?

javascript - 在 PHP 中使用 JS OOP

jquery - jQuery Ajax重定向错误

javascript - 无法在构建期间使用 docker 在 next.js 中发送请求?

java - 带有服务器端 php 的 Android 文件 uploader

php imap 读取正文消息

jquery - 您可以在不更改 html 的情况下覆盖白色和黑色的 Jquery 移动股票图标吗?

javascript - 如何选择用户已加载的文件类型的输入?