D
D
DorF LorS2022-02-11 11:33:11
PHP
DorF LorS, 2022-02-11 11:33:11

Why does Ajax return an error even if the status is 200?

answers[1] = {
    "answid": 12,
    "ans_val": 4325,
    "quest_id": 54,
    }
    answers[2] = {
    "answid": 13,
    "ans_val": 43151,
    "quest_id": 64,
    }
   
            $.ajax({
                url: "/Results.php",
                type: "POST",
                data: {answers: JSON.stringify(answers)},
                dataType: 'json',
                success: function (results) {
                    $.each(results, function (key, val) {
                        if (val.res == "good") {
                        alert("Success");
                        }else if(val.res =="bad"){
                        alert("Fail");
                        }
                    }
                 },
                error: function (err){
                  console.log(err);
            }
            });


if($this->SqlError==null){
        return [
            "data"=>[
                'res' => "good",
                'all_answers' => 3,
                'sub' => 3,
            ],
        ];
    }else{
        return [
            "error"=>$this->SqlError["error"],
        ];
    }

I am trying to get test results. I post Q&A but can't get the result in json format. When dataType: "json" ajax always returns an error, although the results are sent to the database, and when dataType: "text" ajax already accepts it as success BUT it is already as a string and + I receive this response with the data that I sent for processing and because of this some sort of conflict. Someone can help me with this? Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DorF LorS, 2022-02-25
@DorF_0

My problem was that I put extra echo / var_dump / print to check the data. Make sure you only have one echo ajax success

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question