2012-06-20 01:10
浏览 28

jQuery $ .post()函数难以显示结果?

I am using jQuery's $.post function to retrieve some data from a php file, but the returned data does not seem to be showing? Here is what I have got.

Javascript Code:

        //This code works when uncommented, used for debugging.
        /*var x = "tester";

        $.post("import.php", function(data){
            $('input[name=title]').empty().val(data.name); // John
            $('input[name=subtitle]').empty().val(data.time); //  2pm
            }, "json");

PHP import.php Code:

    $my_array = array("name"=>"John","time"=>"2pm");
    echo json_encode($my_array); 

Is there something silly I've missed out, or have I gone about this incorrectly. I'm fairly new to javascript/jquery.

图片转代码服务由CSDN问答提供 功能建议

我使用jQuery的$ .post函数从php文件中检索一些数据,但返回的数据似乎不一样 要展示? 这就是我所拥有的。


  $(document).ready(function  (){
 / * var x =“tester”; 
 $('输入 [name = title]')。val(x); * / 
 $ .post(“import.php”,function(data){
 $('input [name = title]')。empty(  ).val(data.name); // John 
 $('input [name = subtitle]')。empty()。val(data.time); // 2 pm
},“json”); \  n}); 

PHP import.php代码:

 $ my_array = array(“name”=&gt;“John”,“time”=&gt;“2pm”); 
 echo json_encode($ my_array);  

我错过了一些愚蠢的事情,还是我错误地解决了这个问题。 我是javascript / jquery的新手。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpfqy5976 2012-06-20 06:54

    you have to use json.parse() and assign it to a variable to use it as an object.

    download json2.js from here

    json.parse has this documentation.

    a request type of JSON does not mean the ajax function returns the "data" value as an object. it is still a string and needs to be parsed with

    var myObject = JSON.parse(data);

    then you can use it like

    点赞 评论

相关推荐 更多相似问题