I'm trying to post with JQuery like this:
我试图用这样的JQuery发布:
$.post("NiceController/Create/", { field1: data1, field2: data2 },
function(data, textStatus) {
if (data.Status)
//Do something
}, "json");
The problem is, that when I'm not authenticated I don't get redirected, to log-on page, because it's not a full form submit. Question is: How do I, programmatically, know that I'm not authenticated and should redirect to log-on page?
问题是,当我没有经过身份验证时,我不会被重定向到登录页面,因为它不是完整的表单提交。问题是:我如何以编程方式知道我没有经过身份验证,应该重定向到登录页面?
4 个解决方案
#1
I don't know if there's a more official answer, but can you $.get('NiceController/AmILoggedIn')
first? Then, depending on the response in there, $.post
or do some kind of redirect through a login page?
我不知道是否有更正式的答案,但你能先$ .get('NiceController / AmILoggedIn')吗?然后,根据那里的响应,$ .post或通过登录页面进行某种重定向?
#2
If you use the ajax
method, you should be able to add an error handling function. I believe that since you are asking for JSON, a parseerror
ought to be returned since what you will be getting back is a redirect, not JSON. You might also be able to get the same effect using an global ajaxError
handler and continuing to use post.
如果使用ajax方法,则应该能够添加错误处理函数。我相信,既然你要求JSON,那么应该返回一个parseerror,因为你要回来的是一个重定向,而不是JSON。您也可以使用全局ajaxError处理程序并继续使用post来获得相同的效果。
#3
I assume your NiceController/Create/
redirects to the login page, and that is what you're seeing. Instead of a nice status, you get back a bunch of HTML from the login page.
我假设您的NiceController / Create /重定向到登录页面,这就是您所看到的。您可以从登录页面返回一堆HTML,而不是一个好的状态。
You have 2 options: remove the Authorize
attribute from the Create action, and handle it internally, e.g. by returning a JSON error message.
您有两个选项:从“创建”操作中删除“授权”属性,并在内部处理它,例如通过返回JSON错误消息。
Or: check first whether you are logged in, and then call the Create action only when the user is logged in
或者:首先检查您是否已登录,然后仅在用户登录时调用“创建”操作
#4
You could do something like:
你可以这样做:
$.post("NiceController/Create/", { field1: data1, field2: data2 },
function(data, textStatus) {
if (data.Status)
if(data.Status == 'authFailed') {
window.location.href = '/login';
}
}
}, "json");
#1
I don't know if there's a more official answer, but can you $.get('NiceController/AmILoggedIn')
first? Then, depending on the response in there, $.post
or do some kind of redirect through a login page?
我不知道是否有更正式的答案,但你能先$ .get('NiceController / AmILoggedIn')吗?然后,根据那里的响应,$ .post或通过登录页面进行某种重定向?
#2
If you use the ajax
method, you should be able to add an error handling function. I believe that since you are asking for JSON, a parseerror
ought to be returned since what you will be getting back is a redirect, not JSON. You might also be able to get the same effect using an global ajaxError
handler and continuing to use post.
如果使用ajax方法,则应该能够添加错误处理函数。我相信,既然你要求JSON,那么应该返回一个parseerror,因为你要回来的是一个重定向,而不是JSON。您也可以使用全局ajaxError处理程序并继续使用post来获得相同的效果。
#3
I assume your NiceController/Create/
redirects to the login page, and that is what you're seeing. Instead of a nice status, you get back a bunch of HTML from the login page.
我假设您的NiceController / Create /重定向到登录页面,这就是您所看到的。您可以从登录页面返回一堆HTML,而不是一个好的状态。
You have 2 options: remove the Authorize
attribute from the Create action, and handle it internally, e.g. by returning a JSON error message.
您有两个选项:从“创建”操作中删除“授权”属性,并在内部处理它,例如通过返回JSON错误消息。
Or: check first whether you are logged in, and then call the Create action only when the user is logged in
或者:首先检查您是否已登录,然后仅在用户登录时调用“创建”操作
#4
You could do something like:
你可以这样做:
$.post("NiceController/Create/", { field1: data1, field2: data2 },
function(data, textStatus) {
if (data.Status)
if(data.Status == 'authFailed') {
window.location.href = '/login';
}
}
}, "json");