0

I needed to write a function with ajax for rd-mailform, but the string is not parsed. Does anyone know what the problem is?

"@EmailAuth": {
    rule: function(e) {
        email = e.val();
        $.ajax({
            type: "POST",
            url: "auth.php", // php return {"login":1,"status_":0,"user":null}
            async: false,
            cache: false,
            timeout: 30000,
            data: {
                "login": email
            },
            success: function(response) {

                --------------
                var res = JSON.parse(response);
                console.log(response.status_); // not work; why script stop?;
                --------------
                --------------
                var res = JSON.parse('{"login":1,"status_":0,"user":null}');
                console.log(response.status_); // work;  script running continue;
                --------------
            }
        });
        return Boolean(status)
    },
    message: "E-mail not found"
},
Dmitriy
  • 37
  • 1
  • 6
  • Well, try to log the response before the JSON.parse(). Also, do you get any errors? – Aioros Apr 16 '20 at 15:35
  • If the server is putting a Content-Type header on the response of application/json, jQuery will have already parsed it with your method. And trying to parse an already parsed object will error out. – Taplar Apr 16 '20 at 15:36
  • success: function(response) { console.log(response); // return {"login":1,"status":0,"user":null} – Dmitriy Apr 16 '20 at 15:47
  • `console.log(typeof response)` – Taplar Apr 16 '20 at 15:53
  • console.log(typeof response); //return string response = JSON.parse(response); console.log(typeof response); //nothing return – Dmitriy Apr 16 '20 at 15:57
  • auth.php: 1, "status"=>0, "user"=>null)); ?> – Dmitriy Apr 16 '20 at 16:16
  • I tried this: var test = JSON.parse(JSON.stringify(response)); console.log(test); //return {"login":0,"status_":0"user":null}, but console.log(test.status_) //return undefined – Dmitriy 10 mins ago Delete – Dmitriy Apr 16 '20 at 17:48

0 Answers0