簡體   English   中英

如何將編碼的JSON字符串返回給jQuery調用?

[英]How to return an encoded JSON string to jQuery call?

我在PHP AJAX中執行以下操作:

$rows = array();
while($r = mysql_fetch_assoc($sth)) 
{
    $rows[] = $r;
}
print json_encode($rows);

我的JavaScript代碼如下所示:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>

<script type="text/javascript" >
$(function()
{
    $("input[type=submit]").click(function()
    //$("input[type=button]").click(function()
    {
        var name = $("#problem_name").val();
        var problem_blurb = $("#problem_blurb").val();

        var dataString = 'problem_name='+ name + '&problem_blurb=' + problem_blurb;

        if(name=='' || problem_blurb == '')
        {
            $('.success').fadeOut(200).hide();
            $('.error').fadeOut(200).show();
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "/problems/add_problem.php",
                data: dataString,
                success: function()
                {
                    $('.success').fadeIn(200).show();
                    $('.error').fadeOut(200).hide();
                }
            });
        }

        return false;
    });
});
</script>

如何將編碼后的JSON傳輸回jQuery調用,對其進行解碼並輸出該數據? 最好自己親自遍歷數據並通過將字符串混合在一起來制作JSON代碼,這會更好嗎?

謝謝!!

設置dataType:'json'所以您不需要解析json

$.ajax({
 type: "POST",
 dataType:'json',  <----
 url: "/problems/add_problem.php", <---- here you call the php file
 data: dataString,
 success: function(data)  <--- here the data sent by php is receieved
 {
  // data will contain the decoded json sent by server 
  // you can do data.property that depends upon how the json is structured
  $('.success').fadeIn(200).show();
  $('.error').fadeOut(200).hide();
 }
});

add_problem.php

$name=$_POST['problem_name']; // get the name here
$desc=$_POST['problem_blurb']; //get the description here 
$rows = array();
//fetch data from DB
while($r = mysql_fetch_assoc($sth)) 
{
    $rows[] = $r;
}
print json_encode($rows); //json encode it and send back to ajax success handler 
//or
//echo json_encode($rows);

jQuery.getJSON和$ .ajax有一些參數,根據需要傳遞。 “ data:JSON”期望輸出為json格式。 當需要輸出時,需要在成功處理程序中傳遞一個變量。

$.ajax({   
            type: "POST",         
            url: "/problems/add_problem.php",  
            data: JSON,  `datastring is replaced by JSON datatype`
            success: function(data)  `data is json object that will contain the jsonencoded output returned from add_problem.php`
            {  
               for(var i in data)
               {
                   console.log(data[i]) `returns the data at i'th index.`
               }

            }
        });       

只需在您的回調函數中執行

        $.ajax({
            type: "POST",
            url: "/problems/add_problem.php",
            data: dataString,
            success: function( data )
            {
                foreach( var i in data ) 
                 // do something 
            }
        });

最好在服務器端對其進行json編碼,因為在客戶端使用json更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM