简体   繁体   中英

Getting Ajax response (JSON) to PHP Variable

From my form I can successfully send the correct data and receive the correct response, however I am stuck on how I decode the response to a PHP variable so I can manipulate and store it in a session.

I receive my reply like so:

request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log(response);
        request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );

and my responses will be one of the below:

error: 1
error_msg: "Incorrect"
success: 0
tag: "login"

error: 0
success: 1
tag: "login"
user: Object
profileimageurl: "test"
uid: "1"
userEmail: "email@domain.com"
userFirstName: "Test"
userLastName: "User"

How would I get the error message of it fails and the userid if successful?

Edit: Added Request Code

$('#loginSubmit').click(function () {
    //start the ajax

        var f = $("#loginForm");

            request = $.ajax({
                //this is the php file that processes the data 
                url: "app/index.php",
                type: "POST",
                data: f.serialize(),
                dataType:"json",
                cache: false
                  });
        request.done(function (response, textStatus, jqXHR){

        console.log(response);
        request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
         alert("fail")
    });

I have tried $json_decode(response) but this is not defined? Pretty sure I cant use plain old PHP in the javascript? What I basically want is:

request.done(function (response, textStatus, jqXHR){

            myphpvar = response[user][id]
}

Try decode it using parseJSON() function, like the following:

var obj = jQuery.parseJSON( response ); alert( obj.uid );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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