最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing value set inside onSuccess function in Prototype - Stack Overflow

programmeradmin7浏览0评论

I am using Ajax with Prototype library.

Here is my function that calls the Ajax function.

function Testfn()
{

    var DateExists = '';

    new Ajax.Request('testurl',{
            method: 'post',
            parameters: {param1:"A", param2:"B", param3:"C"},
            onSuccess: function(response){
            //DateExists = response.responseText;
                            DateExists = 1;
        }
        });
    // I want to access the value set in the onsuccess function here
    alert(DateExists);

}

When i alert the DateExists value i am getting null value instead of the value that is set in the onsuccess function of my Ajax call which is 1. How is that possible?

Thanks for any help.

I am using Ajax with Prototype library.

Here is my function that calls the Ajax function.

function Testfn()
{

    var DateExists = '';

    new Ajax.Request('testurl',{
            method: 'post',
            parameters: {param1:"A", param2:"B", param3:"C"},
            onSuccess: function(response){
            //DateExists = response.responseText;
                            DateExists = 1;
        }
        });
    // I want to access the value set in the onsuccess function here
    alert(DateExists);

}

When i alert the DateExists value i am getting null value instead of the value that is set in the onsuccess function of my Ajax call which is 1. How is that possible?

Thanks for any help.

Share Improve this question edited Dec 28, 2011 at 21:52 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Aug 7, 2009 at 5:51 ajithmanmuajithmanmu 1,1694 gold badges13 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

The A in AJAX stands for Asynchronous. This means that as soon as you dispatch that Ajax request using new Ajax.Request the request is sent to the server and immediately returns control to your script. Thus, alert(DateExists) will show '' which you initially set.

To see the value of DateExists after returning from the AJAX request, you must move it inside the onSuccess() method.

Example:

function Testfn() {

    var DateExists = '';

    new Ajax.Request('testurl', {
      method: 'post',
      parameters: {param1:"A", param2:"B", param3:"C"},
      onSuccess: function(response){
        DateExists = response.responseText;
        alert(DateExists);
      }
    });
}

The onSuccess callback is executed asynchronously, when the AJAX request ends, so the alert is firing before the callback is called.

You should work with your response, inside the callback or if you want, make another function:

new Ajax.Request('testurl',{
            method: 'post',
            parameters: {param1:"A", param2:"B", param3:"C"},
            onSuccess: function(response){
                        var dateExists = response.responseText;
                        doWork(dateExists);
                        // or alert(dateExists);
                }
        });

function doWork (data) {
    alert(data);
}

CMS is exactly right. The solution is to call the javascript that needs access to DateExists from within the AJAX callback, like this:

function Testfn()
{

  var DateExists = '';

  new Ajax.Request('testurl',{
    method: 'post',
    parameters: {param1:"A", param2:"B", param3:"C"},
    onSuccess: function(response){
      //DateExists = response.responseText;
      DateExists = 1;
      doTheRestOfMyStuff(DateExists);
    }
  });
  // I want to access the value set in the onsuccess function here
  function doTheRestOfMyStuff(DateExists)
  {
    alert(DateExists);
  }
}
发布评论

评论列表(0)

  1. 暂无评论