【问题标题】:Sending PHP json_encode array to jQuery将 PHP json_encode 数组发送到 jQuery
【发布时间】:2011-05-13 13:11:39
【问题描述】:

好的,我想我需要帮助!我搜索了我能想到的每个关键字,但我仍然无法弄清楚,请帮助。我更像是一个 php 人,我刚刚开始使用 jQuery。

基本上,我想做的是从点击函数发送一个 jQuery 帖子。并根据我的 php 函数返回的任何内容,显示/隐藏 2 个 div。我的 php 函数返回一个带有 2 个简单值的“json_encode”数组,如下所示:

//===================PHP代码========================= =========

$message_for_user = "blah blah";
$calculatedValue = 1230;
$responseVar = array(
                    'message'=>$message_for_user,
                    'calculatedValue'=>$calculatedValue
                    );
echo (json_encode($responseVar));

//==================PHP代码结束======================== ==========

我的 javascript 代码应该接受 php 返回的值:

//===================Javascript代码========================= =========

$("div.calculator_result").click(function()
{
    $.post('myCalculator.php' ,{qid:itemID},function(response)
    {
        $("div.calculation_value").show(500).html(response['calculatedValue']);
        $("div#message_for_user").show(500).html(response['message']);
    }
}

//==================Javascript代码结束======================== ==========

不幸的是,在我的项目的 javascript 方面,div 没有使用我的 php 函数返回的值进行更新......我哪里错了?我希望我的问题很清楚,如果没有,请告诉我,我将提供所需的任何额外信息。

另一件事是,早些时候,我只回显一个值,即计算值(echo $calculatedValue),一切正常,只有在我转移到 json 编码数组中的 echo' 之后不工作

【问题讨论】:

    标签: php jquery json


    【解决方案1】:
    var json = $.parseJSON(response); alert(json.message);
    

    【讨论】:

    • 根据您的回答,这是我尝试过的: var resp_object = $.parseJSON(response); $("div.calculation_value").html(resp_object.calculatedValue);不幸的是,这对我有用,伙计。我正在使用 php 版本 5.2.14,如果有帮助的话
    • alrite,这行得通。由于某种扭曲的原因,我的 jquery 版本早于 1.4.1,而且似乎 jQuery.parseJSON 仅在 1.4.1 中添加
    【解决方案2】:

    尝试设置dataType 选项:

    $.post('myCalculator.php' ,{qid:itemID},function(response)
    {
        $("div.calculation_value").show(500).html(response['calculatedValue']);
        $("div#message_for_user").show(500).html(response['message']);
    }, 'json');
    

    注意,我还在你错过的地方添加了右括号 )

    【讨论】:

      【解决方案3】:

      您必须解析 JSON 响应。 jQuery 有这个内置功能(谢天谢地,否则 IE6 和 7 本身不支持 JSON)。设置一个等于这个的变量:

      $.parseJSON(response)
      

      然后,如果您不熟悉 JSON 格式,请检查响应标头(使用 Firebug 或类似方法),这将帮助您选择所需的键值。如果您正在循环播放,我会在解析响应后查看for in statements

      编辑:使用$.getJSON,解析会自动完成。少写,多做。 :)

      【讨论】:

        【解决方案4】:

        您所要做的就是告诉 Ajax 调用您正在接收数据类型“json”。也就是说……

        $.ajax({
           url: "external_file",
           method:"post",
           dataType: "json",  // **************** Note dataType****************
           success:function(response){
               console.log(response)
               // Response will be a javascript array, instead of a string.
           },
           error: function(){
               alert('something went wrong.')
           }
        })
        

        【讨论】:

          猜你喜欢
          • 2011-02-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-22
          • 2011-02-11
          • 2014-10-17
          相关资源
          最近更新 更多