如何从ajax返回值

时间:2012-07-07 12:27:06

标签: jquery ajax

我已经使用jquery ajax从服务器获取值。当方法进入ajax成功或错误时,我需要返回值。如何做到这一点。请指导我。

$.ajax({
    cache: false,
    async: true,
    type: "GET",
    timeout:6000,
    dataType: "json",
    url:url +"log",
    data: { ContactEmail : $("#username").val()},
    contentType: "application/json;charset=utf-8",
    success: function (result) 
    {
        //Here I need to return the result and should get in another method
    },
    Error: function (e)
    {
        //Here I need to return the result and should get in another method
    }
});

更新

例如 当我调用 checkNetConnection()时,返回值。我需要这样

function checkNetConnection() 
{
        var netresult=0;
        var networkState = navigator.network.connection.type;
        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.NONE]     = 'No network connection';
        if(states[networkState]=='Ethernet connection'||states[networkState]=='WiFi connection' || states[networkState]=='Cell 2G connection' || states[networkState]=='Cell 3G connection' || states[networkState]=='Cell 4G connection')
        {
            netresult=1;
        }
        else
        {
            netresult=0;
        }
        return netresult;
}

4 个答案:

答案 0 :(得分:5)

您的代码存在一些问题。您已指定contentType: "application/json;charset=utf-8"但未发送JSON请求。如果要发送JSON请求,请在数据上使用JSON.stringify方法:

data: JSON.stringify({ ContactEmail : $("#username").val() }),

如果没有,请删除此contentType,否则服务器不明确。您的代码的另一个问题是错误回调称为error,而不是Error。就获取结果而言,在成功回调中,它们将直接作为参数传递:

success: function (result) {
    // Here I need to return the result and should get in another method
    anotherMethod(result);
},

还应该注意,如果您的Web服务器设置了正确的内容类型响应头,则传递给success方法的参数将自动由jQuery解析为基础类型。因此,例如,如果您的服务器设置Content-Type: application/json并将{"foo":"bar"}写入响应正文,则可以在此回调中直接操作此对象:alert(result.foo);,而无需额外解析。如果您的Web服务器脚本写得不好并且未设置正确的内容类型标头,例如它设置Content-Type: 'text/html'并将JSON格式的字符串写入响应正文,则可以强制jQuery解析为JSON使用dataType: 'json'参数。但通常情况下,如果一切正确,你就不需要这样做。

现在,error回调是不同的。它作为参数传递xhr对象,因此您必须在此次将结果提取为字符串:

error: function(xhr) {
    anotherMethod(xhr.getResponseText());    
}

如果此字符串代表JSON,您可以解析它:

error: function(xhr) {
    var result = $.parseJSON(xhr.getResponseText());
    anotherMethod(result);    
}

答案 1 :(得分:0)

您的结果存储在变量resulte中:

...
success: function (result) 
    {
        // result contains the results
        foo( results );
    },
    Error: function (e)
    {
        // e contains the error
        bar( e );
    }
...

// Function to do something with the returned results
function foo( someData ) {
  // do stuff
}

// Function to do something with the error
function bar( someErrorData ) {
  // do stuff
}

快速查看数据的方法是使用alert alert( results )alert( e )而不是函数调用来显示数据。

答案 2 :(得分:0)

在成功方法result中是一个json对象,其中包含您从服务器发送的数据。

使用console.log(result)查看数据。

            success: function (result) 
            {
              console.log(result);
            },
            Error: function (e)
            {
            //Here I need to return the result and should get in another method
            }

答案 3 :(得分:0)

  success: function (result) 
            {

              alert(result.someobject); //someobject is returned by your JSON
            },
相关问题