PHP POST在结果正文之前和之后添加了其他字符

时间:2016-01-04 14:15:43

标签: php json ajax

我的php脚本在我的本地电脑上运行良好,但是当它们被上传到测试服务器时,它表现得异乎寻常。 我记得之前在测试服务器中运行过的脚本。

我对脚本进行了调查,并得出结论,每当我向任何 PHP页面提交POST值时,结果会抛出多余的字符,如下所示:

  

d

     

{ “成功”:1}

     

0

这是我用来测试POST的示例脚本,导致上述结果:

<?php
// Turn off all error reporting
error_reporting(0);
echo json_encode(array("success"=>1));

所以基本上,所有的帖子结果都会显示一行显示基数为16的回显字符计数,然后是实际回显输出,最后是另一行显示< strong>数字0 。

我在chrome的inspect元素和chrome的POSTMAN扩展中看到了带有不需要字符的结果体。这就是阻止我所有的ajax json依赖函数工作的原因。

这可能是服务器问题,但我不知道如何测试它以确认我的预感。

通过GET查看脚本(或在浏览器中输入网址)显示没有上述额外字符的预期{"success":1}

任何帮助都将受到高度赞赏。谢谢!

修改

我使用的是Firefox,问题仍然存在。

编辑2

根据请求,这是我使用的许多AJAX函数之一,它在我的测试服务器中不起作用,但在我的本地服务器中工作......

// ...
$.post("costing-calculator.php",
    {   "action": ["retrieve"],
        "name": $("#saveInfo_name").val(),
        "title": $("#saveInfo_title").val(),
        "costing_code": $("#saveInfo_costingCode").val()
    },
    function(data){
        if(data.success){
              console.log(data);
            htmlAlert("Retrieved!", "success");
            $("#retrieveCosting").val("Retrieved!").attr("disabled",null);

            $("input[name=build_type]").prop("checked",null);
            $("input[name=build_type][value='"+data.fields.build_type+"']").prop("checked","checked").change();

            $("#saveInfo_name, #costCalcInput_name").val(data.fields.name || "").change();
            $("#saveInfo_title, #costCalcInput_title").val(data.fields.title || "").change();
            $("#costCalcInput_postcode").val(data.fields.postcode || "").change();
            $("#newInternalArea").val(data.fields.new_internal_area || 0).change();
            $("#renovatedInternalArea").val(data.fields.renovated_internal_area || 0).change();
            $("#newExternalArea").val(data.fields.new_external_area || 0).change();
            $("#renovatedExternalArea").val(data.fields.renovated_external_area || 0).change();
            $("#newDrivewaysPaths").val(data.fields.new_driveways_paths || 0).change();
            $("#newPoolSpa").val(data.fields.new_pool_spa || 0).change();
            $("#renovatedPoolSpa").val(data.fields.renovated_pool_spa || 0).change();

            $("input[name=demolition]").prop("checked",null);
            $("input[name=demolition][value='"+data.fields.demolition+"']").prop("checked","checked");
            if(data.fields.demolition == "Whole house")
                $("#demoWholeHouse").val(data.fields.demo_whole_house_area || 0).change();
            if(data.fields.demolition == "Detailed")
                $("#demoDetailed").val(data.fields.demo_detailed || 0).change();

            $("input[name='excavation[]']").prop("checked",null);
            if(data.fields['excavation[]'] !== undefined && data.fields['excavation[]'].length){
                for (var i = 0; i < data.fields['excavation[]'].length; i++) {
                    $("input[name='excavation[]'][value='"+data.fields['excavation[]'][i]+"']").prop("checked","checked");
                    if(data.fields['excavation[]'][i] == "Bulk Excavation")
                        $("#excavBulk").val(data.fields.excav_bulk || 0).change();
                    if(data.fields['excavation[]'][i] == "Detailed Excavation")
                        $("#excavDetailed").val(data.fields.excav_detailed || 0).change();
                }
            }

            $("#fitOutItemsKitchens").val(data.fields["fit_out_items-Kitchens"] || 0).change();
            $("#fitOutItemsKitchenettes").val(data.fields["fit_out_items-Kitchenettes"] || 0).change();
            $("#fitOutItemsBathrooms").val(data.fields["fit_out_items-Bathrooms"] || 0).change();
            $("#fitOutItemsEnsuites").val(data.fields["fit_out_items-Ensuites"] || 0).change();
            $("#fitOutItemsPowderRooms").val(data.fields["fit_out_items-PowderRooms"] || 0).change();
            $("#fitOutItemsStaircases").val(data.fields["fit_out_items-Staircases"] || 0).change();
            $("#fitOutItemsHomeCinemaRooms").val(data.fields["fit_out_items-HomeCinemaRooms"] || 0).change();
            $("#fitOutItemsLaundry").val(data.fields["fit_out_items-Laundry"] || 0).change();
            $("#fitOutItemsCustom1").val(data.fields["fit_out_items-Custom1"] || 0).change().prop("disabled",data.fields["fit_out_items-CustomName1"] && data.fields["fit_out_items-CustomName1"]!=customFitoutLabel1?false:true);
            $("#fitOutItemsCustomLabel1").text(data.fields["fit_out_items-CustomName1"] || customFitoutLabel1);
            $("#fitOutItemsCustomName1").val(data.fields["fit_out_items-CustomName1"] || customFitoutLabel1);
            $("#fitOutItemsCustomCost1").val(data.fields["fit_out_items-CustomCost1"]).change().prop("disabled",data.fields["fit_out_items-CustomName1"] && data.fields["fit_out_items-CustomName1"]!=customFitoutLabel1?false:true);
            $("#fitOutItemsCustom2").val(data.fields["fit_out_items-Custom2"] || 0).change().prop("disabled",data.fields["fit_out_items-CustomName2"] && data.fields["fit_out_items-CustomName2"]!=customFitoutLabel2?false:true);
            $("#fitOutItemsCustomLabel2").text(data.fields["fit_out_items-CustomName2"] || customFitoutLabel2);
            $("#fitOutItemsCustomName2").val(data.fields["fit_out_items-CustomName2"] || customFitoutLabel2);
            $("#fitOutItemsCustomCost2").val(data.fields["fit_out_items-CustomCost2"]).change().prop("disabled",data.fields["fit_out_items-CustomName2"] && data.fields["fit_out_items-CustomName2"]!=customFitoutLabel2?false:true);
            $("#fitOutItemsCustom3").val(data.fields["fit_out_items-Custom3"] || 0).change().prop("disabled",data.fields["fit_out_items-CustomName3"] && data.fields["fit_out_items-CustomName3"]!=customFitoutLabel3?false:true);
            $("#fitOutItemsCustomLabel3").text(data.fields["fit_out_items-CustomName3"] || customFitoutLabel3);
            $("#fitOutItemsCustomName3").val(data.fields["fit_out_items-CustomName3"] || customFitoutLabel3);
            $("#fitOutItemsCustomCost3").val(data.fields["fit_out_items-CustomCost3"]).change().prop("disabled",data.fields["fit_out_items-CustomName3"] && data.fields["fit_out_items-CustomName3"]!=customFitoutLabel3?false:true);
            $("#fitOutItemsCustom4").val(data.fields["fit_out_items-Custom4"] || 0).change().prop("disabled",data.fields["fit_out_items-CustomName4"] && data.fields["fit_out_items-CustomName4"]!=customFitoutLabel4?false:true);
            $("#fitOutItemsCustomLabel4").text(data.fields["fit_out_items-CustomName4"] || customFitoutLabel4);
            $("#fitOutItemsCustomName4").val(data.fields["fit_out_items-CustomName4"] || customFitoutLabel4);
            $("#fitOutItemsCustomCost4").val(data.fields["fit_out_items-CustomCost4"]).change().prop("disabled",data.fields["fit_out_items-CustomName4"] && data.fields["fit_out_items-CustomName4"]!=customFitoutLabel4?false:true);
            $("#fitOutItemsCustom5").val(data.fields["fit_out_items-Custom5"] || 0).change().prop("disabled",data.fields["fit_out_items-CustomName5"] && data.fields["fit_out_items-CustomName5"]!=customFitoutLabel5?false:true);
            $("#fitOutItemsCustomLabel5").text(data.fields["fit_out_items-CustomName5"] || customFitoutLabel5);
            $("#fitOutItemsCustomName5").val(data.fields["fit_out_items-CustomName5"] || customFitoutLabel5);
            $("#fitOutItemsCustomCost5").val(data.fields["fit_out_items-CustomCost5"]).change().prop("disabled",data.fields["fit_out_items-CustomName5"] && data.fields["fit_out_items-CustomName5"]!=customFitoutLabel5?false:true);
            $("#fitOutItemsCustom6").val(data.fields["fit_out_items-Custom6"] || 0).change().prop("disabled",data.fields["fit_out_items-CustomName6"] && data.fields["fit_out_items-CustomName6"]!=customFitoutLabel6?false:true);
            $("#fitOutItemsCustomLabel6").text(data.fields["fit_out_items-CustomName6"] || customFitoutLabel6);
            $("#fitOutItemsCustomName6").val(data.fields["fit_out_items-CustomName6"] || customFitoutLabel6);
            $("#fitOutItemsCustomCost6").val(data.fields["fit_out_items-CustomCost6"]).change().prop("disabled",data.fields["fit_out_items-CustomName6"] && data.fields["fit_out_items-CustomName6"]!=customFitoutLabel6?false:true);

            $("#totalLengthRobe").val(data.fields.total_length_robe || 0).change();
            $("#totalLengthBespoke").val(data.fields.total_length_bespoke || 0).change();
            $("#totalLengthBalustradingExt").val(data.fields.total_length_balustrading_ext || 0).change();
            $("#totalLengthBalustradingInt").val(data.fields.total_length_balustrading_int || 0).change();

            $("input[name=wiring_home_automation]").prop("checked",null);
            $("input[name=wiring_home_automation][value='"+data.fields.wiring_home_automation+"']").prop("checked","checked").change();

            $("input[name=quality_scale]").prop("checked",null);
            $("input[name=quality_scale][value='"+data.fields.quality_scale+"']").prop("checked","checked").change();

            $("input[name=sloping_site]").prop("checked",null);
            $("input[name=sloping_site][value='"+data.fields.sloping_site+"']").prop("checked","checked").change();

            $("input[name=site_access]").prop("checked",null);
            $("input[name=site_access][value='"+data.fields.site_access+"']").prop("checked","checked").change();

            $("input[name=bushfire_affected]").prop("checked",null);
            $("input[name=bushfire_affected][value='"+data.fields.bushfire_affected+"']").prop("checked","checked").change();

            $("input[name=marine_environment]").prop("checked",null);
            $("input[name=marine_environment][value='"+data.fields.marine_environment+"']").prop("checked","checked").change();

            $("input[name=market_conditions]").prop("checked",null);
            $("input[name=market_conditions][value='"+data.fields.market_conditions+"']").prop("checked","checked").change();

            $("input[name=builder_type]").prop("checked",null);
            $("input[name=builder_type][value='"+data.fields.builder_type+"']").prop("checked","checked").change();

            $("input[name=contingency]").prop("checked",null);
            $("input[name=contingency][value='"+data.fields.contingency+"']").prop("checked","checked").click().change();

            $("#contingencyAmount").val(data.fields.contingency_amount || 0).change();

            $("#costCalcInput_email").val(data.fields.cost_email || "").change();

            $('html, body').animate({
                scrollTop: $("#buildType").offset().top
            }, 500);

            setTimeout(function(){
                $("#retrieveCosting").val("Retrieve Costing");
            }, 5000);
            id = data.id;
        } else{
            htmlAlert(data.error.messages.join('<br/>'),"error", false);

            if(data.error.fields !== null && data.error.fields !== undefined){
                $.each(data.error.fields, function(index, value){
                    notify($("input[name="+value+"]"), data.error.messages[index]);
                });
            }
            $("#retrieveCosting").val("Retrieve Costing").attr("disabled", null);
        }
    },
    "json"
)
// ...

编辑3

响应标头

HTTP/1.1 200 OK
Date: Tue, 05 Jan 2016 01:06:00 GMT
Server: Apache
X-Powered-By: PHP/5.5.30
X-SERVER: 716
Keep-Alive: timeout=5, max=97
Connection: Keep-Alive
Content-Type: application/json
Via: 1.1 UberGlobal-Cloud
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 613

请求标题

POST /0-test-pages/costing-calculator.php HTTP/1.1
Host: www.aaarchitect.com.au
Connection: keep-alive
Content-Length: 52
Pragma: no-cache
Cache-Control: no-cache
Origin: http://www.aaarchitect.com.au
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Referer: http://www.aaarchitect.com.au/0-test-pages/home-building-costing-calculator.html
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: __utma=3608608.1151163053.1424317303.1441072049.1442896000.13; __utmz=3608608.1424317478.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _hp2_id.3414745005=0285466406273470.0064618313.3721268934; _gat=1; BIGipServercbr1lshngpweb-http=2583697580.20480.0000; _hp2_ses.3414745005=*; _hp2_id.3414745005=0285466406273470.3606727761.3721268934; _ga=GA1.3.1151163053.1424317303

表单数据

action%5B%5D=retrieve&name=&title=&costing_code=20ev

响应

4e8
{"error":{"messages":["Invalid costing code."],"fields":["costing_code"]},"id":"c16a5320fa475530d9583c34fd356ef5","success":1,"fields":{"name":"John Cris Test","title":"Test Summary 2","costing_code":"20ev","build_type":"new","year_plan":"2015","new_internal_area":"1","new_external_area":"1","new_driveways_paths":"1","new_pool_spa":"1","demolition":"Detailed","demo_detailed":"2","excavation[]":["Bulk Excavation","Detailed Excavation"],"excav_bulk":"1","excav_detailed":"1","fit_out_items-Kitchens":"1","fit_out_items-Kitchenettes":"1","fit_out_items-Bathrooms":"1","fit_out_items-Ensuites":"1","fit_out_items-PowderRooms":"1","fit_out_items-Staircases":"1","fit_out_items-HomeCinemaRooms":"1","fit_out_items-Laundry":"1","total_length_robe":"1","total_length_bespoke":"1","total_length_balustrading_ext":"1","total_length_balustrading_int":"1","wiring_home_automation":"Full C'bus automation","quality_scale":"3","sloping_site":"Very steep (more than 25 deg)","site_access":"Difficult access","bushfire_affected":"Flame zone affected","marine_environment":"Within 100m of breaking surf","market_conditions":"Recession","cost_name":"John Cris Test","cost_title":"Test Summary 2","cost_postcode":"123","cost_email":"atsalycj@gmail.com","postcode":"123"}}
0

3 个答案:

答案 0 :(得分:1)

您在问题中描述的回复正文的格式特定于chunked transfer encoding。但它应包含标题Transfer-Encoding: chunked而不是Content-Length

我知道这种行为的唯一可能原因是一个不了解HTTP 1.1的代理,并且没有按Transfer-Encoding标题所示解码响应主体。

标题Via表示请求(和响应)通过代理传递,但我无法判断是否应该指责UberGlobal-Cloud。

答案 1 :(得分:0)

也许我的情况可以帮助某人。 错误在于客户端对请求字符串的错误编码。我确实以 win-1251 编码发送了 json,因此服务器尝试将其视为 UTF8 并导致此类伪影。

答案 2 :(得分:-1)

首先包括header('Content-type: application/json'); 然后在浏览器中打开页面(因为你的脚本似乎不依赖于帖子变量)然后在浏览器中检查你的结果,如果它仍在那里那么检查服务器