Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过AngularJS进行ajax调用时无法接收web api中的对象_Angularjs_Json_Ajax_Asp.net Web Api2 - Fatal编程技术网

通过AngularJS进行ajax调用时无法接收web api中的对象

通过AngularJS进行ajax调用时无法接收web api中的对象,angularjs,json,ajax,asp.net-web-api2,Angularjs,Json,Ajax,Asp.net Web Api2,嗨,我正在开发一个小的web api应用程序。两天以来,我一直在努力解决小问题。我无法在web api方法中接收对象。例如,下面是我的web api方法 public IHttpActionResult Put(int id, NCT_UserRegistration users) { } 我正在进行ajax调用,如下所示 this.update = function (sub) { var url = '/NCT_Users/' + sub

嗨,我正在开发一个小的web api应用程序。两天以来,我一直在努力解决小问题。我无法在web api方法中接收对象。例如,下面是我的web api方法

 public IHttpActionResult Put(int id, NCT_UserRegistration users)
        {
        }
我正在进行ajax调用,如下所示

 this.update = function (sub) {
        var url = '/NCT_Users/' + sub.user_id;
        return $http({
            method: 'put',
            data: JSON.stringify(sub),
            url: url,
            contentType: "application/json"
        });
    } 
在上面的Put方法中,我能够接收Id,但无法获取值。它总是接收空值。 这是我的目标

var sub = {
            User_EmailId: $scope.User_EmailId,
            User_Password: $scope.User_Password,
            User_Name: $scope.User_Name,
            User_MobileNum: $scope.User_MobileNum,
            User_Status: $scope.User_Status,
            User_Role: $scope.User_Role,
            User_CreatedDate: new Date(),
            User_UpdatedDate: new Date()
        };
这是来自浏览器的标头请求

URL:http://localhost:22045/NCT_Users/1
Request Method:PUT
Status Code:500 Internal Server Error
Remote Address:[::1]:22045
Response Headers
view source
请求头

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:content-Type, accept, origin, X-Requested-With, Authorization, name
Access-Control-Allow-Methods:POST, PUT, DELETE, GET,OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:130
Content-Type:application/json; charset=utf-8
Date:Tue, 24 Jan 2017 10:50:12 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RjpcTmlyYW5qYW5cTm9vckNvbnN1bHRpbmdTb2x1dGlvblxOb29yQ29uc3VsdGluZ1xOb29yQ29uc3VsdGluZ1xOQ1RfVXNlcnNcMQ==?=
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:145
Content-type:application/json
Host:localhost:22045
Origin:http://localhost:22045
Referer:http://localhost:22045/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

在POST方法中,对象也始终保持null。我的get和delete方法工作正常。我只面对物体的问题。我想从早上开始弄清楚。我可以在这方面得到一些帮助吗。任何帮助都将不胜感激。谢谢。

您不需要字符串化,Angular使其在内部工作

this.update = function (sub) {
        var url = '/NCT_Users/' + sub.user_id;
        return $http({
            method: 'put',
            data: sub,
            url: url,
            headers: {
              "Content-Type": "application/json"
            }
        });
    } 

非常感谢。无论何时我提出诸如post或put之类的请求,我的对象都没有任何价值。对不起。我观察到如下情况。请求URL:请求方法:放置状态代码:500内部服务器错误远程地址:[::1]:22045响应头查看源谢谢。我取下并检查。还是同一个问题。我在我的问题中附上了截图。你检查了你的请求正文了吗?是的。这是请求有效负载。{“User_EmailId”:“dsd”,“User_Name”:“sdsd”,“User_CreatedDate”:“2017-01-24T10:54:23.849Z”,“User_UpdatedDate”:“2017-01-24T10:54:23.849Z”,“User_id”:1}也许这有助于我放置标题:{“Content Type”:“application/x-www-form-urlencoded”},并开始正常工作。