简体   繁体   English

我需要使用jquery从RESTful Web服务检索数据

[英]I need to retrieve data from a RESTful webservice using jquery

i'm new to this RESTful webservice stuff so i'm asking if you can help me with this one. 我是这个RESTful网络服务的新手,所以我问你是否可以帮我这个。

I need to retrieve data from a restful webservice using jquery. 我需要使用jquery从restful webservice中检索数据。 now i know that i can do that using $.ajax or $.getJSON but the question is how can i do it if my restful webservice needs to enter username and password inorder to retrieve the data. 现在我知道我可以使用$ .ajax或$ .getJSON来做到这一点,但问题是如果我的restful webservice需要输入用户名和密码以便检索数据,我该怎么做呢。 Thank you 谢谢

It depends on how your web service does authentication. 这取决于您的Web服务如何进行身份验证。

If it's token-based (via cookies or whatever), you'll need to invoke the login method of your service, get the token (or cookie, etc), and send that token with your future requests. 如果它是基于令牌的(通过cookie或其他),您将需要调用服务的登录方法,获取令牌(或cookie等),并将此令牌与您将来的请求一起发送。

Other methods require other adjustments; 其他方法需要其他调整; authentication headers, etc. We can't tell you what you need unless we know what authentication method the service uses. 除非我们知道服务使用的身份验证方法,否则我们无法告诉您需要什么。

If you have to go to a page and type in a username and password before you can get your data, then it's not going to work. 如果您必须转到页面并输入用户名和密码才能获取数据,那么它就无法运行。 It's not a service at all if this is the case. 如果是这种情况,那根本不是服务。 Not every page is going to be able to be called via AJAX; 并非每个页面都可以通过AJAX调用; usually they can't unless designed with AJAX in mind. 通常他们不能在设计时考虑到AJAX。 "REST" doesn't mean you can call any existing page effectively via AJAX. “REST”并不意味着您可以通过AJAX有效地调用任何现有页面。 It's a service that uses standard URIs and HTTP for it's communication, rather than SOAP etc. (That's a huge simplification, but not a bad one to start with.) 它是一种使用标准URI和HTTP进行通信的服务,而不是SOAP等。(这是一个巨大的简化,但不是一个糟糕的开始。)

Are you in control of this "service"? 你掌控这个“服务”吗? If so, you could: 如果是这样,您可以:

  • rewrite it so that it will accept username/password as query string parameters. 重写它以便它将接受用户名/密码作为查询字符串参数。 Or, 要么,
  • assuming it's on the same site where the page came from where the AJAX is taking place, can you keep the user's session credentials in a cookie? 假设它位于发生AJAX的页面所在的同一站点上,您是否可以将用户的会话凭据保存在cookie中? In many frameworks (.NET in my case) the framework helps you do this and it's relatively easy. 在许多框架中(在我的例子中是.NET),框架可以帮助您完成此任务并且相对容易。 In the site I just deployed, there's REST AJAX all over the place and I don't do anything with Username/Passwords. 在我刚刚部署的站点中,整个地方都有REST AJAX,我对用户名/密码没有做任何事情。 Just handle that up front if possible. 如果可能的话,请尽早处理。

By the way, you're using SSL? 顺便说一下,你在使用SSL? (That is, the site your going to, ajax or otherwise, begins with https?) (也就是说,您要去的网站,ajax或其他方式,以https开头?)

Ideally, you would have some sort of API key or session key. 理想情况下,您将拥有某种API密钥或会话密钥。

Otherwise, you can just pass them as parameters in the call. 否则,您可以将它们作为参数传递给调用。 Just Renee that they are no longer secure. 只是蕾妮,他们不再安全。

You could use the more complex yet more versatile ajax()-function that takes username and password as parameters. 您可以使用更复杂但更通用的ajax() - 将用户名和密码作为参数的函数。 Note however that your users will be able to see the username and password in the source as well as in the request so this will compromise the security of your account. 但请注意,您的用户将能够在源代码和请求中看到用户名和密码,因此这会危及您帐户的安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM