这是我第一次尝试使用 JQuery、Json 并调用 Web 服务。所以请耐心等待。
我需要使用 JQuery 调用 Web 服务,然后返回一个 Json 对象,然后我想将其保存到 JavaScript 变量中。我尝试写一些东西。我只需要有人确认你确实是这样做的。在我实例化它并可能弄乱我公司服务器上的某些东西之前。无论如何,这是:
var returnedJson = $.ajax({
type: 'Get',
url: 'http://172.16.2.45:8080/Auth/login?n=dean&p=hello'
});
就这样,使用 JQuery 调用 Web 服务并将返回的 jsonObject 分配给 JavaScript 变量。谁能确认这是正确的吗?
提前致谢!
最佳答案
var returnedJson = $.ajax({
type: 'Get',
url: 'http://172.16.2.45:8080/Auth/login?n=dean&p=hello'
});
如果你这样做,returnedJson
将是一个 XHR 对象,而不是你想要的 json。您想在成功回调中处理它。像这样的事情:
$.ajax({
// GET is the default type, no need to specify it
url: 'http://172.16.2.45:8080/Auth/login',
data: { n: 'dean', p: 'hello' },
success: function(data) {
//data is the object that youre after, handle it here
}
});
关于javascript - 在 JQuery 中调用 Web 服务并将返回的 Json 分配给 JS 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10715897/