从Jquery AJAX调用返回响应[重复]

时间:2022-10-08 08:24:04

This question already has an answer here:

这个问题在这里已有答案:

I have written a function, which has to check whether a username has been taken or not. Now when I call the function from another function, and alert it's return value:

我写了一个函数,它必须检查是否已经使用了用户名。现在当我从另一个函数调用该函数时,并提示它的返回值:

 alert(checkusernameavailable('justausername')); 

it says 'undefined'. I've searched high and low, but can't find what I'm doing wrong. I guess it should just return the php-echo in check.php, but it doesn't. Here's the function I wrote:

它说“未定义”。我搜索过高低,但找不到我做错了什么。我想它应该只返回check.php中的php-echo,但事实并非如此。这是我写的函数:

var checkusernameavailable = function(value)  {
    $.ajax({
      url: "check.php",
      type: "POST",
      async: false,
      cache: false,
      data: "username=" + value + "",

      success: function(response) {
        alert(response);
        return response;        
      },
      error: function() {
        alert('ajax error');
      }
    });
  } 

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


10  

AJAX calls are async, which means they only return data after the operation has completed. I.e. the method checkusernameavailable never returns any information (unless you tell it to within that method itself). You need to do the following:

AJAX调用是异步的,这意味着它们只在操作完成后返回数据。即checkusernameavailable方法永远不会返回任何信息(除非你在该方法本身内告诉它)。您需要执行以下操作:

// Just fire and forget the method
checkusernameavailable("userName");

// Change the success function to do any display you require
success: function(response) {
    alert(response);
    $("#SomeDiv").html(response);     
  },

The method fires the AJAX async method that posts to check.php. When the response is received, you then handle that response in function associated with the success callback of $.ajax. You can specify a function directly to that success callback as well:

该方法触发发布到check.php的AJAX异步方法。收到响应后,您可以在与$ .ajax成功回调相关的函数中处理该响应。您也可以直接为该成功回调指定一个函数:

// Change success to point to a function name
success: foo

// Create a function to handle the response
function foo(response)
{
   // Do something with response
}

EDIT:

编辑:

As per the OP's comment, you need to change your AJAX call to be synchronous, instead of asynchronous (I've never done a synchronous call like this myself, so this is untested):

根据OP的评论,你需要将你的AJAX调用改为同步,而不是异步(我自己从未做过这样的同步调用,所以这是未经测试的):

var ajaxResponse;

$.ajax({
    async: false, 
    success : function (response)
              {
                  ajaxResponse = response;
              },
    // other properties
});

return ajaxResponse;

Full API listing here.

这里有完整的API列表。

#1


10  

AJAX calls are async, which means they only return data after the operation has completed. I.e. the method checkusernameavailable never returns any information (unless you tell it to within that method itself). You need to do the following:

AJAX调用是异步的,这意味着它们只在操作完成后返回数据。即checkusernameavailable方法永远不会返回任何信息(除非你在该方法本身内告诉它)。您需要执行以下操作:

// Just fire and forget the method
checkusernameavailable("userName");

// Change the success function to do any display you require
success: function(response) {
    alert(response);
    $("#SomeDiv").html(response);     
  },

The method fires the AJAX async method that posts to check.php. When the response is received, you then handle that response in function associated with the success callback of $.ajax. You can specify a function directly to that success callback as well:

该方法触发发布到check.php的AJAX异步方法。收到响应后,您可以在与$ .ajax成功回调相关的函数中处理该响应。您也可以直接为该成功回调指定一个函数:

// Change success to point to a function name
success: foo

// Create a function to handle the response
function foo(response)
{
   // Do something with response
}

EDIT:

编辑:

As per the OP's comment, you need to change your AJAX call to be synchronous, instead of asynchronous (I've never done a synchronous call like this myself, so this is untested):

根据OP的评论,你需要将你的AJAX调用改为同步,而不是异步(我自己从未做过这样的同步调用,所以这是未经测试的):

var ajaxResponse;

$.ajax({
    async: false, 
    success : function (response)
              {
                  ajaxResponse = response;
              },
    // other properties
});

return ajaxResponse;

Full API listing here.

这里有完整的API列表。