简体   繁体   English

如何“理解”我使用Ajax从PHP接收的数据?

[英]How do I “understand” what data I receive from PHP with Ajax?

What I'm doing is returning an RSS feed obtained via a PHP cURL. 我正在做的是返回通过PHP cURL获得的RSS feed。 I return $returnedData which is an associative array that looks something like this: 我返回$ returnedData,它是一个类似以下内容的关联数组:

array(2){
        ["lastupdate"] => string(29) "Tue, 11 Feb 2014 15:25:42 GMT"
        ["feed"] => array(20) {
                    <items contained within ["feed"][0] to ["feed"][19] (very large)> 
                    }
}

I tried to run a json_encode before returning it to Ajax, but all it essentially did was convert my array into one large string. 我试图在将json_encode返回给Ajax之前运行它,但实际上所做的只是将数组转换为一个大字符串。 My Ajax is as follows: 我的Ajax如下:

$.ajaxSetup ({
        cache: false
    });

request = $.ajax ({
    type: "GET",
    url: "dataFetcher.php",
    data: arg,
});


request.done(function(response){
    var resultsArray = response;
    $("#message").text(resultsArray);
});

request.fail(function (jqXHR, textStatus, errorThrown){
    // log the error to the console
    console.error(
        "The following error occured: "+
        textStatus, errorThrown
    );
});

The thing is, I don't exactly know what I'm getting. 问题是,我不完全知道自己要得到什么 If I do var_dump($returnedData) instead of return $returnedData I know that I get a string. 如果我执行var_dump($ returnedData)而不是返回$ returnedData,我知道会得到一个字符串。 But if I simply return $returnedData, I can't tell what Ajax is receiving. 但是,如果我只返回$ returnedData,就无法告诉Ajax正在接收什么。 I tried changing the text displayed by using resultsArray[0] or resultsArray[1] or resultsArray["lastupdate"] hoping that it worked, but it didn't. 我尝试使用resultsArray [0]或resultsArray [1]或resultsArray [“ lastupdate”]来更改显示的文本,希望它能起作用,但没有。

Would really appreciate it if someone could shed some light on what I'm not doing/what I'm doing wrong here. 如果有人可以阐明我在做什么/我在做什么错,我将非常感激。

Thanks! 谢谢!

AJAX receives the response sent by the server. AJAX接收服务器发送的响应。 I'm not a PHP programmer, but from what I've picked up reading other SO questions, you'd need to do something like: 我不是PHP程序员,但是从阅读其他SO问题之后,您需要执行以下操作:

echo json_encode($returnedData)

in your PHP script. 在您的PHP脚本中。 The echo means that it's part of the output, and json_encode converts your PHP array into a JSON string, which represents the array in a way that JavaScript actually understands. echo表示它是输出的一部分,并且json_encode将您的PHP数组转换为JSON字符串,该字符串以JavaScript实际理解的方式表示该数组。

Depending on the headers of the response set in your PHP jQuery may or may not automatically parse the string as JSON, passing the resulting object/array to the success handler (as the value of the first argument) rather than just passing along the string response. 取决于您的PHP中设置的响应标头,jQuery可能会或可能不会自动将字符串解析为JSON,而是将结果对象/数组传递给成功处理程序(作为第一个参数的值),而不仅仅是传递字符串响应。 You can tell it to do that (if you know the response will/should be JSON) by setting the dataType property in your $.ajax() call: 您可以通过在$.ajax()调用中设置dataType属性来告诉它做到这一点(如果您知道响应将/应该是JSON):

request = $.ajax ({
    type: "GET",
    url: "dataFetcher.php",
    data: arg,
    dataType: 'json'
});

Then inside your done handler function response would be the JavaScript object that matches your PHP array. 然后,在done处理程序函数response中将是与PHP数组匹配的JavaScript对象。

Two things: 两件事情:

  1. Use json_encode in your PHP script to encode the data. 在PHP脚本中使用json_encode对数据进行编码。
  2. jQuery is interpreting the response as a string. jQuery将响应解释为字符串。 You can either parse the response string as JSON, or you can take the easier route of setting the AJAX request to expect JSON. 您可以将响应字符串解析为JSON,也可以采用更简单的方法来设置AJAX请求以期望JSON。

So: 所以:

request = $.ajax ({
    type: "GET",
    url: "dataFetcher.php",
    data: arg,
    dataType: 'json' // <--- Your response parameter will be in JSON format by default, now.
});

You need to eval your response to javascript object 您需要评估对javascript对象的响应

request.done(function(response){
    var resultsArray = response.evalJSON();
    $("#message").text(resultsArray);
});

or inside ajax call define that you will receive json 或在ajax调用中定义您将收到json

request = $.ajax ({
    type: "GET",
    url: "dataFetcher.php",
    data: arg,
    dataType: 'json'
});

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

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