简体   繁体   中英

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.

I need to retrieve data from a restful webservice using jquery. 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. Thank you

It depends on how your web service does authentication.

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.

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; usually they can't unless designed with AJAX in mind. "REST" doesn't mean you can call any existing page effectively via 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.)

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? In many frameworks (.NET in my case) the framework helps you do this and it's relatively easy. In the site I just deployed, there's REST AJAX all over the place and I don't do anything with Username/Passwords. Just handle that up front if possible.

By the way, you're using SSL? (That is, the site your going to, ajax or otherwise, begins with https?)

Ideally, you would have some sort of API key or session key.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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