3 Replies - 1123 Views - Last Post: 17 April 2012 - 08:27 AM

#1 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Problem with javascript and ajax request

Posted 13 April 2012 - 01:03 AM

Hi all.I am working in asp.net MVC3 and in client side I am using javascript and ajax to make calls in server and return JSON objects.

I am doing something like this in Localization.js file:

var locale;
load(function () {

    if (xmlhttp.readyState == 4) {

        locale = JSON.parse(xmlhttp.responseText);
        alert(locale); //first alert
    }
});
function load(cfunc) {
    if (window.XMLHttpRequest) {

        xmlhttp = new XMLHttpRequest();
        
    }
    else {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = cfunc;
    xmlhttp.open("GET", 'Default/GetCulture', true);
    xmlhttp.send();
}
alert(locale); //second alert



I also have a Skripta.js file where I use variable "locale" and other thing in Localization.js
The problem is that sometimes I variable locale is "undefined" and I can not use it in Skripta.js.
I reference these files in my index.cshtml(its an html file) like this:
<script src="../../Scripts/Localization.js" type="text/javascript"></script>
<script src="../../Scripts/Skripta.js" type="text/javascript"></script>



P.S. in ajax request in Localization.js the first "alert(locale)" works great, but the second one shows "undefined".

What is the problem ?Does anyone know how can I resolve this problem?

This post has been edited by drilli: 13 April 2012 - 01:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with javascript and ajax request

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Problem with javascript and ajax request

Posted 13 April 2012 - 02:32 AM

Hey.

AJAX stands for "Asynchronous JavaScript And XML". The word to note there is "Asynchronous".

AJAX calls will not happen immediately. When you call: load(cfunc) in your code, you are basically saying: "Do the AJAX request and call the cfunc function when something happens." But it it doesn't wait for that something to happen before moving onto the next line of code. If that next line of code uses a variable that is set in the cfunc callback, it may not be set yet. The AJAX call will more than likely take longer than it takes for the code to reach that next line.

The same can be said about your "Skripta.js" file. If the code in there that uses the locale variable attempts to use the variable before the AJAX request completes, then it will be "undefined".

They key to solving these issues is to wrap whatever code that is meant to use your locale variable into functions, and call those functions in the AJAX callback once the locale variable has been set. You need to put all the code that is meant to use the result of the AJAX request on hold until the AJAX request is completed.
Was This Post Helpful? 1
  • +
  • -

#3 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: Problem with javascript and ajax request

Posted 17 April 2012 - 05:16 AM

Thank You for your reply and your help.I soldved this problem in another way.I just ghanged the request from asynchronous to synchronous, and now other lines of my javascript code will be executed after the request at server is finished.
So now it is everythin OK.

Thank You indeed for your answer.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Problem with javascript and ajax request

Posted 17 April 2012 - 08:27 AM

That is one way to solve it, but it's not generally recommended. By making it synchronous you are freezing the script execution process while it's waiting for the request. That can cause a noticeable lag in your UI if there are other Javascript powered UI effects. - It would be better to leave it to execute asynchronously and just work around it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1