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>

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

The problem is that $.ajax call is asynchronous, a.k.a you check for value of user_found before it is set. try this approach

function myFun(callback){
    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;
                }
            });
            if(typeof callback === "function"){
               callback(user_found);
            }
         },
         error:function () {
              console.log("Error");
          }
        }
    );
}

Then you can use it like

myFun(function(result){
    if (result == true){
        //cool
    }
});

Consider using HTML5 Local Storage. Stored values should be accessible by every page served on your domain.

// Store
localStorage.setItem("user_found", "true");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("user_found");