2012-08-13 63 views
1

我正在开发一个Spotify应用程序,我也试图用JQuery的$ .ajax函数登录Reddit。登录工作,我可以看到开发工具中的响应cookie,但是当我尝试访问Reddit的API的任何部分,要求与呼叫一起发送登录cookie时,它会失败,并且看起来像该cookie永远不会被发送。另外,查看开发工具的“Cookies”部分,表明该网站(应用程序)没有Cookie。Cookie在Spotify(Javascript)中的ajax调用后未保存?

这里的登录电话:

$.ajax({ 
    type: 'POST', 
    url: 'http://www.reddit.com/api/login', 
    data: { 
     'user': $user, 
     'passwd': $password 
    }, 
    success: function() { 
     alert("logged in!"); 
    } 
}); 

而这里的投票调用(userhash从cookie复制的,我可以在开发工具见):

$.ajax({ 
    type: 'POST', 
    url: 'http://www.reddit.com/api/vote', 
    data: { 
     'id': $id, 
     'dir': 1, 
     'uh': $userhash 
    }, 
    success: function(data) { 
     console.log(data); 
    } 
}); 

回答

0

使用xhrFields中添加财产jQuery 1.5.1可能有所帮助:

登录电话:

$.ajax({ 
type: 'POST', 
url: 'http://www.reddit.com/api/login', 
data: { 
    'user': $user, 
    'passwd': $password 
}, 
xhrFields: { 
    withCredentials: true 
}, 
success: function() { 
    alert("logged in!"); 
} 
}); 

投票电话:

$.ajax({ 
type: 'POST', 
url: 'http://www.reddit.com/api/vote', 
data: { 
    'id': $id, 
    'dir': 1, 
    'uh': $userhash 
}, 
xhrFields: { 
    withCredentials: true 
}, 
success: function(data) { 
    console.log(data); 
} 
}); 

这将告诉它的XMLHttpRequest对象的withCredentials属性设置为true,这是需要使用跨域AJAX调用时传递的cookie。

+0

我试过了,它似乎没有改变行为。我是否需要改变有关通话的其他内容? – riggspc 2012-08-20 17:51:38