【问题标题】:Parse a json reply from a jquery result in php从 php 中的 jquery 结果解析 json 回复
【发布时间】:2016-12-20 09:59:15
【问题描述】:

我有一个简单的搜索表单,它使用 jquery 查询外部服务器的结果

$("#formsearch").on("submit", function (event) {
        // everything looks good!
        event.preventDefault();
        submitFormSearch();
});
function submitFormSearch(){
    // Initiate Variables With Form Content
    var searchinput = $("#searchinput").val();
    $.ajax({
        type: "GET",
        url: "https://external-server/api/",
        headers: {"Authorization": "xxxxxxxxxxxxxx"},
        data: "action=Search&query="+searchinput,
                success:function(json){
                    console.log(json);
                    $.ajax({
                        type: "POST",
                        url:'search_func.php',
                        data: "func=parse&json="+json,
                        success:function(data) {
                            console.log(data);
                            $('#risultato_ricerca').html(data);                         
                            }
                    });
                }
    });
}

第一个 GET ajax 工作正常,我得到了正确的数据,但试图在 post 中将此 json 数据发送到我的 php 脚本,但我无法获取数据。 这是 search_func.php 中的代码

if(isset($_POST['func']) && !empty($_POST['func'])){
    switch($_POST['func']){
    case 'parse':
            parse($_POST['json']);
            break;
        default:
            break;
    }
} 
function parse($json) {
    $obj = json_decode($json,true);
    var_dump($obj);
}

... 它显示 NULL

我哪里错了?

编辑: 已解决

更改:
data: "func=parse&json="+json, 至: data: { func: 'parse', json: JSON.stringify(json) },

json 代码正确传递给 search_func.php 将 php 文件中的函数解析更改为:

function parse($json) {
    $data = json_decode(stripslashes($json),true); 
    print_r($data);
}

谢谢。

【问题讨论】:

  • $json 有什么值?
  • $json in function get value from $_POST['json'] in parse($_POST['json']); $_POST['json'] 在 jquery 中获取数据中的值:"func=parse&json="+json, console.log(json);显示codeObject {状态:“ok”,格式:“json”,查询:“acquari”,sort_by:“”,sort_order:“desc”…}code
  • 为什么需要 2 个 ajax 调用?
  • 1° 从外部服务器获取搜索回复 2° 在 php 中详细说明结果

标签: javascript php jquery json ajax


【解决方案1】:

javascript json 变量是否正确填充(即您的控制台会显示什么?)可能您必须在发布之前将 json 变量编码为字符串。

即:代替data: "func=parse&json="+json, 使用data: "func=parse&json="+JSON.stringify(json),

【讨论】:

  • 在控制台中我看到所有来自 1° 的回复 [nimbus.everhelper.me/client/notes/share/694351/… 这是回复:[nimbus.everhelper.me/client/notes/share/694359/…
  • JSON.stringify 部分解决了问题....但现在在 search_func.php 如果我这样做 $data = json_decode($_POST['json'],true); print_r($data); print 给我 null
  • 先尝试转储$_POST['json']。是否有人居住?如果是这样,则解码时出现问题,这可能是由于添加了斜线(取决于 PHP 设置)。 stripslashes 之前 json_decode 应该解决这个问题。
  • 谢谢!! $_POST['json'] 被填充并且stripslashes 解决了这个问题!我终于得到了我的数组:)
【解决方案2】:

看到这个:http://api.jquery.com/jquery.ajax/

正确的语法是:data: { func: 'parse', json: my_json_here }

如果这不起作用可能是您必须将 JSON 编码为字符串(请参阅JSON.stringify()

【讨论】:

  • JSON.stringify 部分解决了问题....但现在在 search_func.php 如果我这样做 $data = json_decode($_POST['json'],true); print_r($data); print 给我 null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 2012-05-02
相关资源
最近更新 更多