Post 方法不允许
Post Method Not Allowed
GET 方法工作完美,但 POST 方法不是。调试时,strReturnValue 变量始终为空。当我继续时,状态是:405 Method Not Allowed。我做错了什么?
在 C# 中,我必须将方法从 POST 更改为选项。
我正在使用 Restangular(angular js)。这是前端函数:
var message = {
Name: new_player.name,
Created: (new Date()).toJSON(),
Affilation: new_player.human,
auth: new_player.auth
}
return Restangular.one('').post('CreatePlayer', message).then(function(){
console.log("Object saved OK");
}, function() {
console.log("There was an error saving");
});
编辑
[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebInvoke(UriTemplate = "CreatePlayer", Method = "OPTIONS", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string CreatePlayer(System.IO.Stream data);
public string CreatePlayer(System.IO.Stream data) {
//convert stream data to StreamReader
System.IO.StreamReader reader = new System.IO.StreamReader(data);
//read StreamReader data as string
string XML_string = reader.ReadToEnd();
string result = XML_string;
//return the XMLString data
return result;
}
必须将我的前端 post-call 的 header 更改为:'application/x-www-form-urlencoded'(默认的 Internet 媒体类型)。
return Restangular.one('').customPOST({
Name: new_player.name,
Created: new Date(),
Affilation: new_player.human,
auth: new_player.fed
}, 'CreatePlayer', {},
{'Content-Type': 'application/x-www-form-urlencoded'
})
或者您可以使用 angular $http 服务:
$http({
url: 'http://localhost:31736/BusinessService.svc/CreatePlayer',
method: 'POST',
data: "test",
headers: {"Content-Type": "application/x-www-form-urlencoded"}
});
就是这样! 然后我可以序列化生成的字符串并继续我的业务逻辑。
public string CreatePlayer(System.IO.Stream data) {
//convert stream data to StreamReader
System.IO.StreamReader reader = new System.IO.StreamReader(data);
//read StreamReader data as string
string XML_string = reader.ReadToEnd();
System.Web.Script.Serialization.JavaScriptSerializer json_serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
BusinessObjects.Player Player = json_serializer.Deserialize<BusinessObjects.Player>(XML_string);
return BL_CreatePlayer.CreatePlayer(Player);
}
GET 方法工作完美,但 POST 方法不是。调试时,strReturnValue 变量始终为空。当我继续时,状态是:405 Method Not Allowed。我做错了什么?
在 C# 中,我必须将方法从 POST 更改为选项。
我正在使用 Restangular(angular js)。这是前端函数:
var message = {
Name: new_player.name,
Created: (new Date()).toJSON(),
Affilation: new_player.human,
auth: new_player.auth
}
return Restangular.one('').post('CreatePlayer', message).then(function(){
console.log("Object saved OK");
}, function() {
console.log("There was an error saving");
});
编辑
[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebInvoke(UriTemplate = "CreatePlayer", Method = "OPTIONS", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string CreatePlayer(System.IO.Stream data);
public string CreatePlayer(System.IO.Stream data) {
//convert stream data to StreamReader
System.IO.StreamReader reader = new System.IO.StreamReader(data);
//read StreamReader data as string
string XML_string = reader.ReadToEnd();
string result = XML_string;
//return the XMLString data
return result;
}
必须将我的前端 post-call 的 header 更改为:'application/x-www-form-urlencoded'(默认的 Internet 媒体类型)。
return Restangular.one('').customPOST({
Name: new_player.name,
Created: new Date(),
Affilation: new_player.human,
auth: new_player.fed
}, 'CreatePlayer', {},
{'Content-Type': 'application/x-www-form-urlencoded'
})
或者您可以使用 angular $http 服务:
$http({
url: 'http://localhost:31736/BusinessService.svc/CreatePlayer',
method: 'POST',
data: "test",
headers: {"Content-Type": "application/x-www-form-urlencoded"}
});
就是这样! 然后我可以序列化生成的字符串并继续我的业务逻辑。
public string CreatePlayer(System.IO.Stream data) {
//convert stream data to StreamReader
System.IO.StreamReader reader = new System.IO.StreamReader(data);
//read StreamReader data as string
string XML_string = reader.ReadToEnd();
System.Web.Script.Serialization.JavaScriptSerializer json_serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
BusinessObjects.Player Player = json_serializer.Deserialize<BusinessObjects.Player>(XML_string);
return BL_CreatePlayer.CreatePlayer(Player);
}