0

I am sending data to php file to check answers.

</script>
    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);
            }
            });
 // PHP code
 if($this->SqlError==null){
        return [
            "data"=>[
                'res' => "good",
                'all_answers' => 3,
                'sub' => 3,
            ],
        ];
    }else{
        return [
            "error"=>SqlError["error"],
        ];
    }

When dataType:"text" it returns (checked in network) {answers[1], aswers[2]},"code":3}{"data":{"res":"good","all_answers":3,"sub":3}

When dataType:"json" it returns {{answers[1], aswers[2]},"code":3} {"error":null}

Does anyone know how to solve the problem?

  • `dataType` specifies how jQuery will try to interpret the response, I don't think it changes anything about the request - so that would not explain why your server-side script responds differently. Have you compared the details of both requests, to check if there are any significant differences nonetheless? – CBroe Feb 11 '22 at 08:05
  • This look the same that this : https://stackoverflow.com/questions/9098649/jquery-ajax-request-with-json-response-how-to/59733878 – svgta Feb 11 '22 at 08:06
  • What is `"error"=>SqlError["error"]` supposed to be? I really rather doubt that a constant array by that name exists. You checked `$this->SqlError` in the condition, so why are you not returning whatever _that_ contains now? – CBroe Feb 11 '22 at 08:07
  • @CBroe I didnt post all code. When there is error while working with db it returns error, but in my case it is null which means there is no error. When I set dataType to Json ajax returns it as error. But when I set it as "Text" it returns success with string data AND it also returns the data I sent (results). I only need "data" as json – MoSalah Lorsanov Feb 11 '22 at 08:13
  • As I said, go and try to figure out in what exact details the requests differ then. If they make the server-side part give a different response, then there must be something I suppose. – CBroe Feb 11 '22 at 08:35
  • You'll still get `success:` when your php returns `"error"=>SqlError["error"]` because the *request* was still a success. You made a POST *request* and that *request* returned something with a 200, so goes in `success:`. There's no way for jquery to know that there was an error in the SQL because it's only interested in the *request*, for which you returned a valid response, so the *request* worked. Check for "error" in your `success:`, probably: `if (data[0].error != "") console.log("sql failed", data[0].error) else { ... sql success` – freedomn-m Feb 11 '22 at 08:56
  • To get to the `error:` *callback* the *request* must fail. Your request is not failing as you are returning valid data. – freedomn-m Feb 11 '22 at 08:58

0 Answers0