简体   繁体   English

jQuery解析JSON响应

[英]jQuery Parse JSON Response

I am successfully Posting to a PHP file, and getting a good response. 我已成功发布到PHP文件,并获得了良好的答复。 The part I cannot seem to get is parsing it out then displaying it on my page. 我似乎无法理解的部分是将其解析然后显示在页面上。 Here is my javascript in a validate handler: 这是我在验证处理程序中的javascript:

submitHandler: function(form) {
    var formData = $(form).serialize();
    $.post('http://test.php', formData, function(data) {
        if(data.success) {
            $('#load').show();
            var response = i;
            $('#load').hide();
            //var msg = '';
            for(var i = 0; i < x.flights.length; i++) {
               msg += '<span>';
               msg += '<p>Flight Number: ' + x.flights[i].flight_number + '</p>';
               msg += '<p>Cost: ' + x.flights[i].cost + '</p>';
               msg += '</span>';
            }
            //this is were I think it should display. but It's not working

            $('#load').html(msg);

Here is my json response: 这是我的json响应:

   success
true

 message
"Success"

flights
[Object { flight_number="334", cost="983.40", departure_city="Kearney Regional Airpor...arney, Nebraska - (EAR)", more...}]

0
Object { flight_number="334", cost="983.40", departure_city="Kearney Regional Airpor...arney, Nebraska - (EAR)", more...}

flight_number
"334"

cost
"983.40"

departure_city
"Kearney Regional Airport, Kearney, Nebraska - (EAR)"

arrival_city
"Chadron Muni Airport, Chadron, Nebraska - (CDR)"

departs
"2014-03-19 04:33:00"

arrives
"2014-03-19 08:12:00"

duration
"219"

adult_seats_available
"2"

senior_seats_available
"1"

I know you you are not seeing the JSON response, but I can see it in FF firebug. 我知道您没有看到JSON响应,但可以在FF firebug中看到它。 I'm new to jQuery/JSON, and I just want to print the response to my page. 我是jQuery / JSON的新手,我只想将响应打印到我的页面上。 Thanks in advance. 提前致谢。

Look into JSON.stringify(data) doc I don't know where you want to display your JSON but if you just want to see what it is open up the debugger and console.log(JSON.stringify(data)); 查看JSON.stringify(data) 文档,我不知道您想在哪里显示JSON,但是如果您只想查看它的内容,请打开调试器和console.log(JSON.stringify(data)); should do it. 应该这样做。

You are using post method and it may default return xml, json, script, text, html. 您正在使用post方法,它可能默认返回xml,json,脚本,文本,html。 So you are getting the data from post method. 因此,您正在从post方法获取数据。

Try looking at https://api.jquery.com/jQuery.post/ 尝试查看https://api.jquery.com/jQuery.post/

And if you want to access it, then you can use JSON.stringify(data) to show json as a string. 如果要访问它,则可以使用JSON.stringify(data)将json显示为字符串。 And to access data from json you can use dot(.) notation. 要从json访问数据,您可以使用点(。)表示法。

There's a couple of things to look at here. 这里有几件事情要看。

First you're adding your html (ie msg ) to the #load element ( $('#load').html(msg); ) but earlier in the code you're hiding it ( $('#load').hide(); ). 首先,您将html(即msg )添加到#load元素( $('#load').html(msg); )中,但在代码的前面,您将其隐藏( $('#load').hide(); )。 So I think this will answer your main question; 因此,我认为这将回答您的主要问题; ie remove the the line $('#load').hide(); 即删除行$('#load').hide(); or add $('#load').show(); 或添加$('#load').show(); after the line $('#load').html(msg); $('#load').html(msg); .

Still not seeing anything? 还是没看到什么? Then perhaps the response isn't as corrent as you're claiming so perhaps an easier way to check what's been assigned to msg is to alert the html alert(msg); 然后,响应可能不如您所声称的那么正确,因此,检查分配给msg内容的一种更简单的方法可能是提醒html alert(msg); <- make this call after you've built your html. <-构建html后进行此调用。

So aside from this is the use of the #load element you seem to be using it to hold a loading gif but also assigning it the response via the content of msg . 因此,除了使用#load元素外,您似乎正在使用它来保存加载的gif,还通过msg的内容为其分配了响应。 Perhaps you need to separate the use of the elements so that the #load element is for the loading gif and you add another element for the response. 也许您需要分开使用元素,以便#load元素用于加载gif,并为响应添加另一个元素。 so you're code is more like this. 所以你的代码更像这样。

html html

<div id="load" class="hide"><img src="loading-animation.gif">...</div>
<div id="post-response" class="hide alert"><div>

where the hide class is display none and alert class represents an style for a response alert. 隐藏类不显示警报类表示响应警报的样式。

js js

submitHandler: function(form) {

    // show the loading gif before we make the 
    // post data and wait for an async response
    $('#load').show();

    ...
    $.post('http://test.php', formData, function(data) {
        if(data.success) {

            // post-back has completed so lets hide the animation
            $('#load').hide();

            // your code to build html msg

            // assign and show the response element
            $('post-response').html(msg);
            $('post-response').show();
            ...

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

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