首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向asp.net web服务器发送角post请求

向asp.net web服务器发送角post请求
EN

Stack Overflow用户
提问于 2014-08-04 08:14:08
回答 1查看 3K关注 0票数 1

在角度js中,我试图发送一个post请求,她是控制器。.controller('ActivationController',['$http','$location','$routeParams','AuthService',

代码语言:javascript
运行
复制
function($http, $location, $routeParams, AuthService){

        var location = $location.path();
        var activation_code = $routeParams.code;
        var activationLink = "http://localhost:18678/api/User/ActivateUser";
        console.log(activation_code);
        if(activation_code){
            $http({method:"post", url:activationLink, data:activation_code}).success(function(response){
                console.log(response);
            }).error(function(error){
                console.log(error);
            });
        }

    }]);

在asp.net web中,her是一种方法。

代码语言:javascript
运行
复制
 [HttpPost]
        public HttpResponseMessage ActivateUser([FromBody]string activation_code)
        {
            if (!string.IsNullOrWhiteSpace(activation_code))
            {
                string decode_token = HttpUtility.UrlDecode(activation_code); ;
                string token_string = Crypto.Decrypt(activation_code, passPhrase);

                if (token_string != null)
                {
                    User activateAcc = db.Users.Where(user => user.ConfirmToken == token_string).SingleOrDefault();
                    if (activateAcc != null)
                    {
                        activateAcc.IsActive = true;

                        try
                        {
                            db.SaveChanges();
                            var credential = new UserCredential(); 
                            credential.EmailAddress = activateAcc.UserMail; 
                            credential.Password = activateAcc.UserPassword;

                            return Request.CreateResponse(HttpStatusCode.OK, credential);
                        }
                        catch
                        {
                            return Request.CreateResponse(HttpStatusCode.Ambiguous, "cannot confirm account");
                        }
                    }
                    else
                    {
                        return Request.CreateResponse(HttpStatusCode.NotAcceptable, "invalid account");
                    }
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NoContent, "invalid token data");
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NoContent, "missing activation code");
            }
        }

问题是当控制器发出请求时,但是没有向服务器发送任何数据。[FromBody]string activation_code为空

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-04 08:23:49

用引号包装您的参数:

代码语言:javascript
运行
复制
$http({method:"post", url:activationLink, data: '"' + activation_code + '"'});

解释

要使Web API绑定到简单的字符串原语,必须将主体指定为:

“这里有一根绳子”

例如:

代码语言:javascript
运行
复制
POST http://localhost:5076/api/values HTTP/1.1
 User-Agent: Fiddler
 Host: localhost:5076
 Content-Type: application/json
 Content-Length: 7

 "Alice"

引号很重要。有关更多信息,请查看此文章

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25114291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档