This is my first time using an API, so this might be pretty basic, but I'm pretty m开发者_StackOverflow中文版uch pulling at strings here.
I have a URL which accepts a username and password in JSON format through the post method, this is to essentially verify the client before any real work can happen. If the username and password are accepted, the response will contain a status code and a cookie for the session. That cookie needs to be sent with each subsequent request. That last part is whats confusing me.
Currently here is what I have:
<script>
$.post("https://login-url", { "username": "testuser","password":"1234567890" },
function(data){
$.cookie("test", data);
alert(data.username); // John
console.log(data); // 2pm
}, "json");
$.post("https://register-url", { "username": "test@aol.com","password":"password","attributeMap":{"firstName":"ETest","lastName":"NTest","languageCode":"EN"} },
function(data){
alert(data.username); // John
console.log(data); // 2pm
}, "json");
</script>
So I'm trying to set the cookie in the first part (I can't tell if I'm doing it right?), and I have really no idea how to send it along in the second post.
Part of the issue is, the API is brand new, and has little to no documentation.
Thanks!
-Elliot
(also, this code is copied from jquery's docs and then just modified a bit to meet my needs)
$.POST is asynchronous - which means that the second post will fire immediately after the first. This means that the second post will fire before the cookie has been set.
Either do the second post in the callback function of the first OR set a global variable to check if the cookie has been set and loop with a timer until the cookie has been set.
精彩评论