如何返回从阿贾克斯
值我已经使用了jQuery AJAX从server.when,方法进入阿贾克斯成功或错误获取值,我需要返回value.How做that.Please指导我。如何返回从阿贾克斯
$.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(),它返回的value.i需要像这样
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;
}
你的结果存储在变量result
和e
:
...
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(results)
或alert(e)
而不是函数调用。
在成功方法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
}
您的代码有几个问题。您已指定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);
},
还应当指出的是,传递给成功方法的参数会自动jQuery的解析到基础类型,如果您的Web服务器设置了正确的内容类型响应头。因此,例如,如果您的服务器设置了Content-Type: application/json
并将{"foo":"bar"}
写入响应正文,则可以在此回调中直接操作此对象:alert(result.foo);
而无需额外解析。如果您的Web服务器脚本写得不好并且没有设置正确的内容类型标题,例如它设置了Content-Type: 'text/html'
并在响应正文中写入了JSON格式的字符串,那么可以使用参数dataType: 'json'
将jQuery强制解析为JSON 。但通常情况下,如果一切正确,则不需要这样做。
现在,error
回调是不同的。它是通过xhr
对象作为参数,所以你将不得不提取结果作为字符串这个时候:
error: function(xhr) {
anotherMethod(xhr.getResponseText());
}
如果该字符串代表JSON,你可以分析它:
error: function(xhr) {
var result = $.parseJSON(xhr.getResponseText());
anotherMethod(result);
}
+ 1 ..完全热衷于看待的东西:) – 2012-07-07 12:32:15
我更新我的代码,好心的通过它 – JavaH 2012-07-07 12:38:26
我不明白你在问什么。 AJAX是异步的,这意味着你不能使用AJAX请求从函数返回任何东西。您可以使用此AJAX请求的结果的唯一地方是成功回调。这是绝对重要的,每个开发AJAX的Web开发人员都必须知道。如果你不明白这个基本原理,我邀请你阅读更多关于AJAX是什么以及它是如何工作的。 – 2012-07-07 12:39:57
success: function (result)
{
alert(result.someobject); //someobject is returned by your JSON
},
我更新我的代码,好心经过它 – JavaH 2012-07-07 12:40:11
如何返回这个值? – JavaH 2012-07-07 12:29:42
@JavaH AJAX会自动返回一个值。你想显示一个结果还是什么? – 2012-07-07 12:30:17
我更新我的代码,好心的通过它 – JavaH 2012-07-07 12:39:44