使用Angular发布时未设置参数

时间:2014-08-19 14:54:23

标签: angularjs post asp.net-web-api

我有一个MVC Web API端点

[HttpPost]
public ReadQuoteForClientIdResponse ReadQuoteForClientId
        (
        ReadQuoteForClientIdRequest readQuoteForClientIdRequest
        )
{
    return new ReadQuoteForClientIdResponseMapper().Map
    (
        new ReadQuoteWorkflowHelper().ReadForClientId(readQuoteForClientIdRequest.ClientId)
    );
}

端点将ReadQuoteForClientIdRequest作为参数

public class ReadQuoteForClientIdRequest
{
    public int ClientId { get; set; }
}

我试图做一个简单的帖子并且到达控制器,但由于某种原因,ClientId始终为0。

function quoteGridController($scope, $http) {
$scope.quotes = [];

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$http.post("http://localhost:57048/service/Quote/ReadQuoteForClientId", { "ClientId": "2" })
    .success(function(data) {

    })
    .error(function(data) {
    alert("Error occured " + data);
});
}

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我将参数传递给$.Param来解决问题

$http.post("http://localhost:57048/service/Quote/ReadQuoteForClientId", $.param({ "ClientId": 2 }))
    .success(function(data) {

    })
    .error(function(data) {
    alert("Error occured " + data);
});