简体   繁体   English

如何将编码的JSON字符串返回给jQuery调用?

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

I do something like this in my PHP AJAX: 我在PHP AJAX中执行以下操作:

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

My calling JavaScript code is like this: 我的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>

How can I transfer the encoded JSON back to the jQuery call, decode it, and output that data? 如何将编码后的JSON传输回jQuery调用,对其进行解码并输出该数据? And would it be better to have just looped through the data myself and made the JSON code by concatinating the string together? 最好自己亲自遍历数据并通过将字符串混合在一起来制作JSON代码,这会更好吗?

Thanks!! 谢谢!!

set the dataType:'json' so you dont need to parse the 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 and $.ajax have some parameters, that are passed as per need. jQuery.getJSON和$ .ajax有一些参数,根据需要传递。 "data : JSON" expects output to be in json format. “ data:JSON”期望输出为json格式。 And when you need output, you need to pass a variable in success handler. 当需要输出时,需要在成功处理程序中传递一个变量。 ie

$.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.`
               }

            }
        });       

Just do it in your callback function 只需在您的回调函数中执行

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

It's better to json encode it on the server side because it's easier to work with json on the client side. 最好在服务器端对其进行json编码,因为在客户端使用json更容易。

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

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