Asp.net mvc 向asp.net mvc发送json ajax post请求时出错
我正在将sencha touch的ajax请求发送到我的asp.net mvc服务:Asp.net mvc 向asp.net mvc发送json ajax post请求时出错,asp.net-mvc,extjs,sencha-touch-2,http-status-code-405,Asp.net Mvc,Extjs,Sencha Touch 2,Http Status Code 405,我正在将sencha touch的ajax请求发送到我的asp.net mvc服务: Ext.Ajax.request({ url: 'http://localhost:52771/api/login', method: 'post', defaultHeaders : 'application/json', params: { post: JSON.stringify ({
Ext.Ajax.request({
url: 'http://localhost:52771/api/login',
method: 'post',
defaultHeaders : 'application/json',
params: {
post: JSON.stringify ({
user: username,
pwd: password
})
},
success: function (response) {
var loginResponse = Ext.JSON.decode(response.responseText);
if (loginResponse === true) {
// The server will send a token that can be used throughout the app to confirm that the user is authenticated.
me.sessionToken = loginResponse.sessionToken;
me.signInSuccess(); //Just simulating success.
} else {
me.signInFailure(loginResponse.message);
}
},
failure: function (response) {
me.sessionToken = null;
me.signInFailure('Login failed. Please try again later.');
}
});
以下是服务代码:
Namespace SimpleSevice
Public Class LoginController
Inherits ApiController
Private Shared list As IList(Of Login1) = New List(Of Login1)( _
{New Login1() With {.user = "User", .pwd = "Password"}, _
New Login1() With {.user = "User1", .pwd = "Password1"}, _
New Login1() With {.user = "User2", .pwd = "Password2"}
}
)
<System.Web.Mvc.AcceptVerbs("GET")> _
Public Function GetLogin() As IEnumerable(Of Login1)
Return list
End Function
<System.Web.Mvc.HttpPost> _
Public Function PostLogin(ByVal login As Login1) As Net.Http.HttpResponseMessage
If login.user = "John" AndAlso login.pwd = "Human" Then
Return Request.CreateResponse(Of Boolean)(Net.HttpStatusCode.OK, True)
End If
Return Request.CreateResponse(Of Boolean)(Net.HttpStatusCode.NotFound, False)
End Function
End Class
End Namespace
你能帮我吗 看起来您需要在Web API上启用跨源资源共享。默认情况下,运行应用程序的web浏览器将不允许您调用其他域(其他端口被视为跨域)。下面是关于如何在Web API 2上执行此操作的详细信息 编辑:如果您使用的是Web API 1。。。
谢谢,现在它可以工作了,但在我的服务中,登录什么都不做。
OPTIONS http://localhost:52771/api/login?_dc=1391588046145 405 (Method Not Allowed)
OPTIONS http://localhost:52771/api/login?_dc=1391588046145 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
XMLHttpRequest cannot load http://localhost:52771/api/login?_dc=1391588046145. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.