我从sencha touch发送json到ASP.NET MVC:
Ext.Ajax.request({
url: 'http://localhost:52771/api/login',
method: 'post',
headers: {
'Content-Type': '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"}
}
)
Public Function GetLogin() As IEnumerable(Of Login1)
Return list
End Function
<System.Web.Http.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
结束命名空间
这是http流量:
Request URL:http://localhost:52771/api/login?_dc=1391752685427
Request Method:POST
Status Code:500 Internal Server Error
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:53
Content-Type:application/json
Host:localhost:52771
Origin:http://localhost:1841
Referer:http://localhost:1841/login/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
_dc:1391752685427
Request Payload
post=%7B%22user%22%3A%22t%22%2C%22pwd%22%3A%22y%22%7D
Response Headers
Access-Control-Allow-Origin:http://localhost:1841
Cache-Control:no-cache
Content-Length:900
Content-Type:application/json; charset=utf-8
Date:Fri, 07 Feb 2014 05:58:18 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcc2VuY2hhXFNpbXBsZVNldmljZVxTaW1wbGVTZXZpY2VcYXBpXGxvZ2lu?=
我一直没有登录。
答案 0 :(得分:1)
ASP.NET MVC和ASP.NET Web API是两个完全不同的框架。他们可以并排坐在同一个项目中,但他们使用完全不同的类。您在ApiController
中的操作所使用的属性来自MVC命名空间,因此它们无效。
答案 1 :(得分:1)
如果您要使用默认标头,请在处理请求之前将其与此Ext.Ajax
一起使用。
Ext.Ajax.defaultHeaders = {
'Content-Type' : 'application/json'
};
如果要为每个请求指定标头,请使用此。
Ext.Ajax.request({
url: 'http://localhost:52771/api/login',
method: 'post',
headers: { 'Content-Type' : 'application/json' }, // rest of the code
基本上,您需要确保在请求中发送Content-Type
请求标头,其值为application/json
。
但是,正如Darrel所指出的,您正在使用ASP.NET MVC框架中的属性。它仍然可以在您的情况下正常工作,因为您已根据约定正确命名了操作方法,并且属性根本没有进入图片。使用System.Web.Http.HttpPost
。更好的是,只需删除它们。
更新:
而不是
params: {
post: JSON.stringify({
user: username,
pwd: password
})
},
使用
jsonData: { user: 'john', pwd: 'pass' }