Angular $ http.post 1.6.1没有将数据传递给WebAPI

时间:2017-02-03 18:17:50

标签: angularjs angular-http

所有,我刚刚使用1.6.1创建了一个新的Angular包,但现在数据似乎没有传递给我的WebAPI。但是,当我通过SoapUI或类似的东西发布位时,一切都很好。

Javascript看起来像这样:

function testapi()
        {
            var serviceRoot='http://server/testangular16/api/Values';

            var deferred=$q.defer();

            var req = {
                method: 'POST',
                url: serviceRoot,
                data: 'PassInTheText'
            };

            $http(req).then(goodResponse,badResponse);

            return deferred.promise;
        };
    function goodResponse(response)
    {
        console.log("Good response");
        console.log(response);

    }

    function badResponse(response)
    {
        console.log("Bad response");
        console.log(response);
    }

和webapi是一个非常简单的C#控制器:

    // POST api/values
    public HttpResponseMessage Post([FromBody]string value)
    {
        HttpResponseMessage rp = new HttpResponseMessage(HttpStatusCode.OK);
        rp.Content = new StringContent(value);

        return rp;
    }

我正在进入控制器,我可以设置一个断点并点击我可以看到值的部分。它总是空的。

查看网络跟踪,角度部分确实进行了预检,我可以看到200响应。

Request URL:http://server/testangular16/api/Values
Request Method:OPTIONS
Status Code:200 OK
Remote Address:10.7.14.209:80
**Response Headers view source**
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:content-type
Access-Control-Allow-Origin:http://localhost:8000
Cache-Control:no-cache
Content-Length:0
Date:Fri, 03 Feb 2017 18:09:04 GMT
Expires:-1
Pragma:no-cache
Server:"Management Corporation"
X-AspNet-Version:4.0.30319
**Request Headers view source**
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:mjvzrx3
Origin:http://localhost:8000
Referer:http://localhost:8000/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

所以,看起来我正在传递CORS,但是当我尝试将数据作为帖子的内容传递时,它就没有成功。

我缺少什么想法?

谢谢, 尼克

2 个答案:

答案 0 :(得分:0)

Web API需要邮件正文中的对象,除非使用application/x-www-form-urlencoded作为内容类型,并使用等于=符号的值作为前缀,否则您无法传递基本类型。

所以你可以通过其中一种方法修复它

  1. 调整c#参数并将其转换为具有字符串属性的类型,然后发送带有匹配参数名称的json对象。
  2. 将请求更改为url-encoding内容类型,并将=添加到变量值。
  3. 将其作为URL的一部分而不是邮件正文发送,您仍然可以使用POST方法。
  4. 更改为form-urlencoded

    function testapi()
    {
        var serviceRoot='http://server/testangular16/api/Values';
        var deferred=$q.defer();
    
        var req = {
            method: 'POST',
            url: serviceRoot,
            data: '=PassInTheText', // added =
            contentType: 'application/x-www-form-urlencoded' // specify content type
        };
    
        $http(req).then(goodResponse,badResponse);
        return deferred.promise;
    };
    

答案 1 :(得分:0)

看来如果我这样做,它会按预期工作......想法?

CREATE TABLE jsontext(x json);

INSERT INTO jsontext VALUES ($${"a":1}$$::text);
ERROR:  column "x" is of type json but expression is of type text
LINE 1: INSERT INTO jsontext VALUES ($${"a":1}$$::text);

CREATE CAST (text AS json)
  WITH INOUT
  AS ASSIGNMENT;

INSERT INTO jsontext VALUES ($${"a":1}$$::text);
INSERT 0 1