简体   繁体   English

jQuery AJAX JSON问题

[英]jQuery AJAX JSON issue

On the first page I have this function: 在第一页上,我具有以下功能:

 <script>
function update() {
  $("#notice_div").html('Loading..'); 


  $.ajax({
    type: 'GET',
    dataType: 'json',
    data: latestid,
    url: '2includejson.php?lastid='+ latestid + '',
    timeout: 4000,
    success: function(data) {


      $("#cont_div").html(data);
      $("#cont_div").clone().prependTo($("#newdiv")); 
      $("#notice_div").html(''); 
      $("#cont_div").html('');
      window.setTimeout(update, 4000);

    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      $("#notice_div").html('Timeout contacting server..');
      window.setTimeout(update, 60000);
    }
});
}
$(document).ready(function() {
    update();

}); 
</script>

And some php. 和一些PHP。

The included file: 包含的文件:

    <? 
     header("Content-Type: application/json", true);

    $la = $_GET['lastid'];  
    include ("../../setup.php");

    $jsonArray[] = array();

      $count = 1; // first message is the newest on load
      $get = $DB->query("SELECT * FROM board WHERE id>'$la' ORDER BY id DESC LIMIT 5", __FILE__, __LINE__);
    while ($msg = $DB->fetch_array($get))
    {   

  if($count == 1){ 
  $latestid = $msg['id']; // newest message - this I need to pass to the other page
  }
  $count++; 

    $jsonArray = "$msg[msg]";
    }
    echo json_encode($jsonArray);
    ?>

I'm just trying to learn how to use ajax and jquery. 我只是想学习如何使用ajax和jquery。

As you see, I pass latestid as js variable through the URL url: '2includejson.php?lastid='+ latestid + '', 如您所见,我通过URL url将lastestid作为js变量传递:'2includejson.php?lastid ='+ latestid +'',

I need to renew/post pack a newer value from the included page but I have no idea how to do so. 我需要从包含的页面中更新/发布打包一个新值,但是我不知道该怎么做。 Before using json I could just overwrite it with javascript, but now I don't know... The newer value will then be posted again as latestid. 在使用json之前,我可以用javascript覆盖它,但是现在我不知道...较新的值将作为最新ID再次发布。

You can declare the array without [] : 您可以声明不带[]的数组:

$jsonArray = array();

Also you should then append the data to the array instead of making a string: 另外,您还应该将数据附加到数组中,而不是创建字符串:

$jsonArray[] = $msg['msg'];

And in the end: 最后:

$jsonArray['latestid'] = $latestid;

Then in JavaScript, you should declare latestid : 然后在JavaScript中,您应该声明latestid

var latestid;

And inside the ajax function, you should just pass the data as an object, and not twice like you're doing now. 在ajax函数内部,您应该只将数据作为对象传递,而不是像现在这样两次。 And just replace latestid there, which has been returned in JSON format: 只需替换那里的latestid ,它以JSON格式返回:

...
data: {lastid: latestid},
url: '2includejson.php',
timeout: 4000,
success: function(data) {
    latestid = data.latestid; // update latestid
    ...
}
...
success: function(data) {

the data in here is a json object. 这里的data是一个json对象。 so u cant do $("#cont_div").html(data); 所以你不能做$("#cont_div").html(data); u have to read the json and then write it to a div like this; 您必须读取json,然后将其写入这样的div;

$.each(data,function(key,value){
    $("#cont_div").append(key + " "+ value);
})

Try this 尝试这个

success: function(data) {


      $("#cont_div").html(data);
      $("#cont_div").clone().prependTo($("#newdiv")); 
      $("#notice_div").html(''); 
      $("#cont_div").html('');
  latestid = data.latestid ; // update the value
      window.setTimeout(update, 4000);

    }

You might have to use the $.parseJSON; 您可能必须使用$ .parseJSON;。

Suppose the server returns a json of the form {"id":"2"}, then in the success function do 假设服务器返回了{“ id”:“ 2”}形式的json,然后在成功函数中执行

success:function(data){
var s=$.parseJSON(data);
alert(s.id)-->will alert 2

And I'm not sure if your server script returns a properly formated json. 而且我不确定您的服务器脚本是否返回格式正确的json。 Use firebug figure this out 使用萤火虫找出答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM