本文介绍了在JQuery中调用Web服务并将返回的Json分配给JS变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试使用JQuery,Json并调用Web服务.

我需要使用JQuery调用webserivce,然后返回一个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
    }
});

This is my first time attempting working with JQuery, Json AND calling a web service.. So please bear with me here.

I need to call a webserivce using JQuery, that then returns a Json object that I then want to save to a javascript variable. I've attempted writing a little something. I just need someone to confirm that this is indeed how you do it. Before I instantiate it and potentially mess up something on my company's servers. Anyways, here it is:

var returnedJson = $.ajax({
    type: 'Get',
    url: 'http://172.16.2.45:8080/Auth/login?n=dean&p=hello'
});

So there it is, calling a webservice with JQuery and assigning the returned jsonObject to a javascript variable. Can anyone confirm this is correct?

Thanks in advance!

解决方案
var returnedJson = $.ajax({
    type: 'Get',
    url: 'http://172.16.2.45:8080/Auth/login?n=dean&p=hello'
});

If you do it like this returnedJson would be an XHR object, and not the json that youre after. You want to handle it in the success callback. Something like this:

$.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
    }
});

这篇关于在JQuery中调用Web服务并将返回的Json分配给JS变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:04