简体   繁体   English

Php 对 ajax 请求的响应错误 json

[英]Php with wrong json response on ajax request

i'm not a php developer, i'm doing this to help a friend.我不是 php 开发人员,我这样做是为了帮助朋友。 I have an issue with my ajax response from my php file.我的 php 文件中的 ajax 响应存在问题。 In my HTML i make an ajax request在我的 HTML 我提出 ajax 请求

function addNewBrand() {
    disableAllButton();

    var excludeBrandOnFacebook = $('#facebook_brand_excluded').is(":checked");
    var excludeBrandOnTrovaprezzi = $('#trovaprezzi_brand_excluded').is(":checked");

    if(excludeBrandOnFacebook || excludeBrandOnTrovaprezzi) {
        var outOfStockArray = getOutOfStockValueArray();
        var categories = getExcludedCategory();

        var newBrand = getNewBrandToExclude();
        var brands = getExcludedBrand();
        brands.push(newBrand);

        var obj = { excludeOutOfStock: outOfStockArray, brands: brands, categories: categories};
        var rulesJSON = JSON.stringify(obj);

        console.log(rulesJSON);

        $.ajax({
            url: '/save_rules.php',
            type: 'POST',
            dataType: "json",
            data: rulesJSON,
            cache: false,
            success: function(data) {

                if(data.status == 'success'){
                    appendRow(newBrand);
                    alert("Complete!");
                    $( '#brand_to_append').val('');
                    $( '#trovaprezzi_brand_excluded' ).prop('checked', false);
                    $( '#facebook_brand_excluded' ).prop('checked', false);
                } else if(data.status == 'error'){
                    alert("An error occours! " + data.msg);
                }

                enableAllButton();
            },
            error: function( result ) {
                console.log(result);
                alert("Ops, something went wrong. " + result.msg);
                enableAllButton();
            }
        });
    } else {
        enableAllButton();
    }
}

And process it in my save_rules.php in this way并以这种方式在我的 save_rules.php 中处理

#!/usr/bin/php
<?php 
    if( session_status() != PHP_SESSION_ACTIVE ){ 
        session_start();
    }

    //require platform core files
    require_once ($_SERVER['DOCUMENT_ROOT'] . "/wp-load.php");
    require_once ($_SERVER['DOCUMENT_ROOT'] . "/wp-includes/pluggable.php");
    //include(ABSPATH . "wp-includes/pluggable.php");
    ini_set("memory_limit", '2048M');

    header('Content-type: application/json');

    $rawdata = file_get_contents("php://input");
    // Let's say we got JSON
    $decoded = json_decode($rawdata);

    $file = $_SERVER['DOCUMENT_ROOT']. "/rules.json";
    $jsonFile = fopen( $file, "w+" );
    fwrite($jsonFile, $rawdata);
    fclose($jsonFile);

    $post_array = array();

    if( $rawdata != null ) {
        $post_array[] = ['code'=>200, 'status'=>'success'];
    } else {
        $post_array[] = ['code'=>400, 'status'=>'error'];
    }

    echo json_decode( $post_array );

?>

Until few days ago everythings works but now i got an error.直到几天前一切正常,但现在我得到了一个错误。 Actually i receive a 200 status code response but in my js function error section is fired instead of success.实际上我收到 200 状态代码响应,但在我的 js function 错误部分被触发而不是成功。 The problem, i think, is on my JSON response.我认为问题出在我的 JSON 响应上。 I checked on chrome console and i get this from server我检查了 chrome 控制台,我从服务器上得到了这个

#!/usr/bin/php
{"code":200,"status":"success"}

Why this happen?为什么会这样? And why this not happens until few days ago?为什么直到几天前才发生这种情况? All your answer will be appreciated.您的所有回答将不胜感激。

Your mistake was in your save_rules.php file here:您的错误出现在您的 save_rules.php 文件中:

#!/usr/bin/php
<?php

In fact when you load this file it output firt the string " #!/usr/bin/php " and after play the PHP code.实际上,当您加载此文件时,它 output 会先输入字符串“ #!/usr/bin/php ”,然后播放 PHP 代码。 So you response are not JSON valid because before your JSON you got the string " #!/usr/bin/php ".因此,您的响应不是 JSON 有效,因为在您的 JSON 之前,您得到了字符串“ #!/usr/bin/php ”。

So remove this string from your PHP code and your JSON will be OK.所以从你的 PHP 代码中删除这个字符串,你的 JSON 就可以了。

One more thing at the end of your PHP file use: PHP 文件末尾的另一件事是:

echo json_encode( $post_array );

Because you want to send JSON right?因为您要发送 JSON 对吗? Not a decoded JSON.不是解码的 JSON。

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

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