I am trying to send an AJAX request to a Rails controller that uses before_filter
to authenticate the user. The problem is when the request gets sent, the server asks for authentication even if the user is already logged in.
我正在尝试向使用before_filter对用户进行身份验证的Rails控制器发送AJAX请求。问题是当请求被发送时,服务器请求身份验证,即使用户已经登录。
Here is my AJAX request:
以下是我对AJAX的请求:
$.ajax({
type: 'DELETE',
url: "/messages/delete_all",
data: [1,2,4],
success: function() {
console.log("Your request is received.")
}
});
When the user hits delete_all
from the browser, it works, but with AJAX it doesn't. What gives?
当用户从浏览器中点击delete_all时,它就可以工作了,但是使用AJAX就不行。到底发生了什么事?
EDIT: could it be related to the authenticity_token
?
编辑:它是否与authenticity_token相关?
1 个解决方案
#1
1
Maybe this question will help: Using javascript with the twitter API
也许这个问题会有帮助:在twitter API中使用javascript
The solution proposed there should look something like this in your case
这里提出的解决方案应该是这样的
$.ajax({
type: 'DELETE',
url: "/messages/delete_all",
data: [1,2,4],
beforeSend: function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password)
},
success: function() {
console.log("Your request is received.")
}
});
#1
1
Maybe this question will help: Using javascript with the twitter API
也许这个问题会有帮助:在twitter API中使用javascript
The solution proposed there should look something like this in your case
这里提出的解决方案应该是这样的
$.ajax({
type: 'DELETE',
url: "/messages/delete_all",
data: [1,2,4],
beforeSend: function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password)
},
success: function() {
console.log("Your request is received.")
}
});