简体   繁体   English

如何从ajax成功回调函数读取javascript变量

[英]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 响应是html的混合内容,还包含

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

if(loginStatus !== 'false') does not work in the success. if(loginStatus!=='false')不能成功运行。 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. 如果响应为JSON,则将更加容易。 Why can't I parse a Ajax html GET response in jQuery? 为什么我不能在jQuery中解析Ajax html GET响应? provides hints to a potential solution. 提供了潜在解决方案的提示。

You will have to parse the HTML that gets returned. 您将必须解析返回的HTML。 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. 糟糕的是,除非您执行一些正则表达式魔术来获取变量的值,否则Javascript部分必须为eval d(不好的做法!)。

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. 还有一个带有基本思想的JSFiddle 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. 基本上,您使用JQuery的.parseHTML方法解析响应,并使用.globalEval评估脚本,此时可以检查变量。

$.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 另一个参考: jQuery-HTML中的脚本标签由jQuery解析并且不执行

I think you can't access a variable inside ajax return because its javascript is not interpreted. 我认为您无法在ajax返回值内访问变量,因为它的javascript无法解释。

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: 这不是一个好主意(您可能会遇到其他问题),但是可以将返回值放到一些隐藏的iframe中,并使用类似以下内容的方法来访问其中的变量:

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结果的页面,建议您仅响应变量内容或json,以便在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 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. 我使用PHP脚本语言解释了此登录概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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