admin 管理员组

文章数量: 1087132

This is my javascript code:

function getData(param1,param2) {
    $.ajaxSetup({async:false});
    var request = $.ajax({
        url: ".php?p1="+param1+"&p2="+param2,
        type: "POST",
        async: false,
        success: function(imageurl){
            return imageurl;
        }
    });
}

and here is the PHP page in which, I am calling this function:

<html>
<head>
     <script src="jquery-1.9.1.js"></script>
        <script src=".js"></script>
        <script>

            function callMyFunction(box) {
                alert(box);
                box.value = "Waiting....";
                var data = getData('param1', 'param2'); 
                box.value = data;
                alert(data);    //here I am getting "Undefined"
            }
        </script>
</head>
<body>
        <?=$_SERVER["REMOTE_ADDR"]?><br />
        <input type="button" onclick="callMyFunction(this)" value="Click Here" />
</body>
</html>

Now When I am calling this getData() function in My php page then it returns "Undefined".

When I alert data in JavaScript code ,it alerts correct value but still "undefined" in PHP page.

It is an Ajax ASYNC problem but even after setting it false , I am still facing this problem.

I debugged it with firebug and I can see that the URL (from where I am getting the data using ajax) is returning correct value but it is not receiving on PHP page where I am calling this function.

Any suggestions to make this work

This is my javascript code:

function getData(param1,param2) {
    $.ajaxSetup({async:false});
    var request = $.ajax({
        url: "http://someurl./xyz.php?p1="+param1+"&p2="+param2,
        type: "POST",
        async: false,
        success: function(imageurl){
            return imageurl;
        }
    });
}

and here is the PHP page in which, I am calling this function:

<html>
<head>
     <script src="jquery-1.9.1.js"></script>
        <script src="http://myjsdomain./myjs.js"></script>
        <script>

            function callMyFunction(box) {
                alert(box);
                box.value = "Waiting....";
                var data = getData('param1', 'param2'); 
                box.value = data;
                alert(data);    //here I am getting "Undefined"
            }
        </script>
</head>
<body>
        <?=$_SERVER["REMOTE_ADDR"]?><br />
        <input type="button" onclick="callMyFunction(this)" value="Click Here" />
</body>
</html>

Now When I am calling this getData() function in My php page then it returns "Undefined".

When I alert data in JavaScript code ,it alerts correct value but still "undefined" in PHP page.

It is an Ajax ASYNC problem but even after setting it false , I am still facing this problem.

I debugged it with firebug and I can see that the URL (from where I am getting the data using ajax) is returning correct value but it is not receiving on PHP page where I am calling this function.

Any suggestions to make this work

Share Improve this question edited Apr 30, 2014 at 14:24 Jay Blanchard 34.4k17 gold badges80 silver badges126 bronze badges asked Apr 30, 2014 at 14:17 DespicableDespicable 3,9673 gold badges28 silver badges43 bronze badges 6
  • Change POST to GET since you're passing your data in the url. – Jack Commented Apr 30, 2014 at 14:21
  • From the docs - As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/plete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success(). – Jay Blanchard Commented Apr 30, 2014 at 14:21
  • @JayBlanchard can you provide the code by using done? – Despicable Commented Apr 30, 2014 at 14:22
  • Is the getData() function also on the PHP page, or included through a file anywhere? From that Undefined error it seems not. – Styphon Commented Apr 30, 2014 at 14:25
  • possible duplicate of How to return the response from an AJAX call? – Bergi Commented Apr 30, 2014 at 14:29
 |  Show 1 more ment

2 Answers 2

Reset to default 4

You have to return result from function getData, return in success function is not enough - it's in diferent scope

function getData(param1,param2) {
  var result;
  var request = $.ajax({
    url: "http://someurl./xyz.php?p1="+param1+"&p2="+param2,
    type: "GET",
    async: false,
    success: function(imageurl){
      result = imageurl;
    }
  });
  return result;
}

Or you can do it asynchronously this way:

function changeValue(box, param1, param2) {
  box.value = "Waiting....";
  var request = $.ajax({
    url: "http://someurl./xyz.php?p1="+param1+"&p2="+param2,
    type: "GET",
    async: true,
    success: function(imageurl){
      box.value = imageurl;
    }
  });
  return result;
}
function callMyFunction(box) {
  alert(box);
  changeValue(box, 'param1', 'param2')
}

Just as an alternative to the accepted answer, here's how to do it properly in an asynchronous way with promises. See the duplicate question How do I return the response from an asynchronous call? for more explanation.

function getData(param1, param2) {
    return $.ajax({
        url: "http://someurl./xyz.php",
        data: {p1:param1, p2:param2},
        type: "GET" // I think this is fine since you didn't send any data
    });
}
function callMyFunction(box) {
    alert(box);
    box.value = "Waiting....";
    getData('param1', 'param2').then(function(imageurl) { 
        box.value = imageurl;
        alert(imageurl);
    });
}

本文标签: javascriptjquery ajax async is not working accordinglyStack Overflow