简体   繁体   中英

how to read javascript variable from ajax success callback function

I am doing

$.ajax({
    type: "GET",
    url: url, //valid url..The ajax part works fine
    dataType: 'html',
    success: function(response) {

        if (loginStatus !== 'false') {
            testFunction(response); //assuming there is a function called testFunction
        }
    }
});

The response is a mixed content of html and it also contains

<script>var loginStatus = 'false'</script>

if(loginStatus !== 'false') does not work in the success. What am I doing wrong?

The response looks like this

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>

<body>
This is a test page
<script>var loginStatus = 'false'</script>
</body>

</html>

The solution is not pretty. This would be much easier if the response was JSON. Why can't I parse a Ajax html GET response in jQuery? provides hints to a potential solution.

You will have to parse the HTML that gets returned. The bad part is that the Javascript part will have to be eval d (bad practice!) unless you do some regular expression magic get the value of the variable.

UPDATE (DON'T DO THIS):

Again, disclaimer, this is bad practice , but it's possible. The following is just a proof of concept applied to your code. There is also a JSFiddle with the basic idea. Basically, you parse the response with JQuery's .parseHTML method and evaluate the script with .globalEval at which point the variable becomes available to check.

$.ajax({
  type: "GET",
  url: url, //valid url..The ajax part works fine
  dataType: 'html',
  success: function(response) {
    var scriptContent = $($.parseHTML(response, document, true)).filter('script').text();
    $.globalEval(scriptContent);
    // at this point, the variable loginStatus should be defined

    if (loginStatus !== 'false') {
        testFunction(response); //assuming there is a function called testFunction
    }
  }
});

Another reference: jQuery - script tags in the HTML are parsed out by jQuery and not executed

I think you can't access a variable inside ajax return because its javascript is not interpreted.

It's not a good idea (and you could have others problems) but you can put the return to some hidden iframe and access the variable inside using something like:

document.getElementById("iframeId").contentWindow.loginStatus

If you have control of the page that you are retrieving the ajax result, I recommend you to response only the variable content or a json to manipulate easily in your javascript.

$.ajax({
    type: "GET",
    url: 'loginCheck.php', //valid url..The ajax part works fine
    dataType: 'html',
    success: function(response) {
        if (response == "YES") {
         testFunction(response); //assuming there is a function called testFunction
        }
    }
});

loginCheck.php

// check the username and password if it matches with the username and password in the database . then echo "YES" otherwise echo "NO"
i explained this login concept using PHP scripting language.

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