I am trying to retrieve values from a database by using JQuery Ajax to call a web service routine. The web service routine is set up properly and returns the value I require, however I'm confused about how the jquery ajax works... I don't know how to retrieve the value
我试图通过使用JQuery Ajax调用web服务例程从数据库中检索值。web服务例程被正确设置并返回我需要的值,但是我对jquery ajax的工作方式感到困惑……我不知道如何检索值
$.ajax({
type: 'POST',
url: 'myservices.asmx/getRowName',
dataType: 'xml',
data: ({ param1: someData, param2: someData }),
success: function(data) {
alert( data.getElementsByTagName("string")[0].firstChild.data );
},
error: function(msg) { alert(msg.statusText); }
});
So on the success, I can access the data returned through that function (I've checked and it is returning the right data), but what if I want to use the data outside of that function?
因此,在成功的时候,我可以访问通过这个函数返回的数据(我已经检查过了,它正在返回正确的数据),但是如果我想使用函数之外的数据呢?
Does $.ajax
return anything where I could retrieve the data I need? such as...
美元。ajax返回任何可以检索所需数据的内容?如……
data = $.ajax({...all the settings...});
or...
还是……
data = $.ajax({...settings...}).responseText;
Any ideas?
什么好主意吗?
2 个解决方案
#1
3
The problem with what you're suggesting you'd like to do is that AJAX is an asynchronous process and trying to assign the result of the AJAX call to a variable like that would suggest the code would need to "hang" there until the result comes back. That's not how things are done with Javascript. Most of the time I do whatever I need to do with my data in the success callback itself. If that's not enough, the way to do it would be to have a function that accepts the data as an argument and call it when you are done. This may seem weird to you if you're coming from other languages, but in Javascript things are a lot more event based ("do this when this happens"), so you have to setup your code accordingly:
您建议您要做的问题是,AJAX是一个异步进程,并试图将AJAX调用的结果分配给一个变量,这就意味着代码需要“挂起”在那里,直到结果返回。这不是用Javascript完成的事情。大多数时候,我在成功回调本身中对数据做任何需要做的事情。如果这还不够,方法是让一个函数接受数据作为参数,并在完成时调用它。如果你来自其他语言,这可能会让你觉得很奇怪,但在Javascript中,事情更多地是基于事件(“发生这种情况时就这么做”),所以你必须相应地设置代码:
function doStuffWithData(data) {
// do whatever you want here
}
$.ajax({
type: 'POST',
url: 'myservices.asmx/getRowName',
dataType: 'xml',
data: ({ param1: someData, param2: someData }),
success: function(data) {
doStuffWithData(data);
},
error: function(msg) { alert(msg.statusText); }
});
#2
0
Please find a better example using xml in JQuery http://codernd.com/DevelopersFile/Reading-XML-file-using-JQuery.aspx
请在JQuery http://codernd.com/developersfile/read- xml -file- use - jquery.aspx中找到更好的示例
#1
3
The problem with what you're suggesting you'd like to do is that AJAX is an asynchronous process and trying to assign the result of the AJAX call to a variable like that would suggest the code would need to "hang" there until the result comes back. That's not how things are done with Javascript. Most of the time I do whatever I need to do with my data in the success callback itself. If that's not enough, the way to do it would be to have a function that accepts the data as an argument and call it when you are done. This may seem weird to you if you're coming from other languages, but in Javascript things are a lot more event based ("do this when this happens"), so you have to setup your code accordingly:
您建议您要做的问题是,AJAX是一个异步进程,并试图将AJAX调用的结果分配给一个变量,这就意味着代码需要“挂起”在那里,直到结果返回。这不是用Javascript完成的事情。大多数时候,我在成功回调本身中对数据做任何需要做的事情。如果这还不够,方法是让一个函数接受数据作为参数,并在完成时调用它。如果你来自其他语言,这可能会让你觉得很奇怪,但在Javascript中,事情更多地是基于事件(“发生这种情况时就这么做”),所以你必须相应地设置代码:
function doStuffWithData(data) {
// do whatever you want here
}
$.ajax({
type: 'POST',
url: 'myservices.asmx/getRowName',
dataType: 'xml',
data: ({ param1: someData, param2: someData }),
success: function(data) {
doStuffWithData(data);
},
error: function(msg) { alert(msg.statusText); }
});
#2
0
Please find a better example using xml in JQuery http://codernd.com/DevelopersFile/Reading-XML-file-using-JQuery.aspx
请在JQuery http://codernd.com/developersfile/read- xml -file- use - jquery.aspx中找到更好的示例