对于php json响应,iron-ajax响应返回null

时间:2022-10-17 14:29:12

I'm a beginner in polymer 1.0 in the following codes I'm trying to get json response from my php file when the iron-ajax's on-response triggers. I have tried different response by editing php, but it always shows null in my console log. ajax part of the code

我是以下代码中的聚合物1.0的初学者我正试图从我的php文件中获取json响应时,铁-ajax的响应触发器。我通过编辑php尝试了不同的响应,但它总是在我的控制台日志中显示为null。 ajax代码的一部分

<iron-ajax
        id="ajax"
        method="POST"
        url="post_tip.php"
        handle-as="json"
        on-response="postResponse"
        >
    </iron-ajax>

script part of code to handle on-response

脚本部分代码来处理响应

postResponse: function(r){
            console.log(r.detail.response);
            if(r.detail.response.success==1){
                console.log('Tip posted');
            }
            else{console.log('Error occured.not posted');}
        }

I can't find any error but r.detail.response returns null every time.

我找不到任何错误,但r.detail.response每次都返回null。

on my php I have.

在我的PHP我有。

$resp = '{"success":1}';
echo $resp;
echo json_encode($resp);

can't figure out which part I'm doing wrong. sorry for the possible dumb question.

无法弄清楚我做错了哪一部分。抱歉可能是愚蠢的问题。

1 个解决方案

#1


1  

You doing it all wrong in php. First of all json_encode will encode an array or an object to json string, it will escape your string, but thats not that what you want. Secondly you returning twice without [ ] bracket which cannot be parsed with JSON.parse or cannot be used as a JSON Object in javascript.

你在php中做错了。首先,json_encode会将数组或对象编码为json字符串,它会转义你的字符串,但这不是你想要的。其次,在没有[]括号的情况下返回两次,无法使用JSON.parse进行解析,或者无法在javascript中用作JSON对象。

You should send back JSON content-type, because the response may be not hooked trough JSON.parse, and expecting a real JSON, but on the iron-ajax documentation the handle-as="json" means:

你应该发回JSON内容类型,因为响应可能没有通过JSON.parse挂钩,并期望一个真正的JSON,但在iron-ajax文档中,handle-as =“json”意味着:

json: uses XHR.responseText parsed as JSON.

json:使用解析为JSON的XHR.responseText。

So the iron-ajax element using JSON.parse on the XHR.responseText to parse the JSON as an object, that means the PHP header is not required but you should use.

所以在XHR.responseText上使用JSON.parse的iron-ajax元素将JSON解析为一个对象,这意味着不需要PHP头,但你应该使用它。

Change your php code something like this:

改变你的PHP代码是这样的:

<?php

    $result = array('success' => 1);

    header('Content-Type: application/json');
    echo json_encode($result);

#1


1  

You doing it all wrong in php. First of all json_encode will encode an array or an object to json string, it will escape your string, but thats not that what you want. Secondly you returning twice without [ ] bracket which cannot be parsed with JSON.parse or cannot be used as a JSON Object in javascript.

你在php中做错了。首先,json_encode会将数组或对象编码为json字符串,它会转义你的字符串,但这不是你想要的。其次,在没有[]括号的情况下返回两次,无法使用JSON.parse进行解析,或者无法在javascript中用作JSON对象。

You should send back JSON content-type, because the response may be not hooked trough JSON.parse, and expecting a real JSON, but on the iron-ajax documentation the handle-as="json" means:

你应该发回JSON内容类型,因为响应可能没有通过JSON.parse挂钩,并期望一个真正的JSON,但在iron-ajax文档中,handle-as =“json”意味着:

json: uses XHR.responseText parsed as JSON.

json:使用解析为JSON的XHR.responseText。

So the iron-ajax element using JSON.parse on the XHR.responseText to parse the JSON as an object, that means the PHP header is not required but you should use.

所以在XHR.responseText上使用JSON.parse的iron-ajax元素将JSON解析为一个对象,这意味着不需要PHP头,但你应该使用它。

Change your php code something like this:

改变你的PHP代码是这样的:

<?php

    $result = array('success' => 1);

    header('Content-Type: application/json');
    echo json_encode($result);