weixin_33725272 2016-02-03 22:11 采纳率: 0%
浏览 24

Ajax Call的结果[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr">How do I return the response from an asynchronous call?</a>
                            <span class="question-originals-answer-count">
                                (38 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-02-04 03:11:45Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

I have an ajax call and I want to use result of this ajax on another page.

//This is main.js file

function myFun(){
    var my_user = "Stas";
    var itemsUrl = "myxmlwithusers";
    var user_found = false;
    $.ajax({
         url: itemsUrl,
         method: "GET",
         headers: { "Accept": "application/json; odata=verbose" },
         cache: false,
         async: false,
         dataType: "text",
         success: function(data){
            var jsonObject = JSON.parse(data);
            results = jsonObject.d.results;
            $(results).each(function(){ 
                if($(this)[0].user == my_user){
                     user_found = true;
                }
            });
         },
         error:function () {
              console.log("Error");
          }
        }
    );
}

// This is execute.js file

myFun();
if (user_found == true){cool}

I need true or false for // user_found. Let say I will put in the file execute.js my function myFun() and in main.js my user is fetch with "$(this)[0].user" and I need to receive "true" in execute.js

Thanks!

</div>
  • 写回答

3条回答 默认 最新

  • Didn"t forge 2016-02-03 22:18
    关注

    1st: simply in html before including any js files

    <head>
       <script>
          var user_found = true; // or false if you like
       </script>
       // include main.js and execute.js
    </head>
    

    now you can call user_found anywhere in js files without var .. even it change in any js files functions it will change in others

    Simple Example

    2nd: If you include main.js before execute.js you can use it like

    var user_found = false;
    function myFun(){
        var my_user = "Stas";
        var itemsUrl = "myxmlwithusers";
        $.ajax({
             url: itemsUrl,
             ....................
    

    Simple Example

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?