get the Ajax return value

Asked 1 months ago, Updated 1 months ago, 6 views

How do I get the Ajax return value?
When submitting, Ajax will go to see the DB and the return value will indicate the error or proceed as it is.
(The code itself is broken in many ways below.)

html    
<form action="NEXTPATH"...>
   <input type="text" id="email">
   <input type="submit" id="submit" value="send">
</form>
<divid="error"></div>

So, in JS,

$(function(){
    $("#submit").click(function(){
        function testajax(){
            return$.ajax({
                type: 'get',
                datatype: 'json',
                url: 'api.php',
            });
        }

        var re=testajax().done(function(data){
            console.log(data['status']);
            if(data['status']=='OK'&data['message']==false){
                return false;
            }
        });

        console.log(re);
        if(re==false){
            $("#error").html('Error.');
        }
        return;
    });
});

Like this, api.php simply returns the value in json format.

{"status":"OK", "message":true}

Ajax itself works well and can take the value of json, but the expected return value from the function testajax() is not returned.
As it is asynchronous, the order of execution does not go as expected, and at this time console.log(re); is running first, returns OBJECT object, and then console.log(data['status']); is running.Also, testajax() will return [Object {readyState:1}] instead of the returned value.
1. Communicate via Ajax and make various decisions based on the returned values.
2. I want to return the value as the return value of the function.
That's all, but it's not going well.

testajax().done(function(data){
....
}

It works fine, but I want to return false (or other values are acceptable), but it doesn't return, so the submit action eventually progresses because it can't control false and true.

Please tell me who knows more.
Thank you for your cooperation.

javascript php json ajax

2022-09-30 11:11

3 Answers

Is it okay if there are no countermeasures such as pushing twice?
First of all, put false in the return value and cancel once
Why don't you send it from javascript after that?
Otherwise, I can't recommend it very much, but
You should stop the browser using async:false.
I think it's complicated because we use asynchronous communication and synchronous communication.

$(function(){
    $("#submit").click(function(){
        function testajax(){
            return$.ajax({
                type: 'get',
                datatype: 'json',
                url: 'api.php',
            });
        }

        var failed = function(res){
            $("#error").html('Error.');
        };

        testajax().done(function(data){
            if(data['status']=='OK'&data['message']==false){
                failed(false);
            } else {
                // Submit Form
                document.form.submit();
            }
        }).fail(failed);

        return false;
    });
});


2022-09-30 11:11

The result of jQuery.ajax() is that jQuery determines success, error, so

$.ajax({
   type: 'get',
   datatype: 'json',
   url: 'api.php',
   success:function(result){
     // Action after receiving a response.The result argument contains the data received.
   } 
});

Please describe what you want to do or call in the success section like this.


2022-09-30 11:11

before
data type: 'json',

after
dataType: 'json',


2022-09-30 11:11

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.