Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.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
Javascript AngularJS将数据作为键值对发布到asp.net页面_Javascript_C#_Asp.net_Angularjs - Fatal编程技术网

Javascript AngularJS将数据作为键值对发布到asp.net页面

Javascript AngularJS将数据作为键值对发布到asp.net页面,javascript,c#,asp.net,angularjs,Javascript,C#,Asp.net,Angularjs,我正在尝试将键值对数据从angular应用程序发布到asp.net页面,但无法在asp.net页面接收结果 示例数据 {fullName: "Vikas Bansal", userId: "1001387693259813", userimageurl: "http://graph.facebook.com/1001387693259813/picture?width=250&height=250"} 角度代码 var postData = { fullNam

我正在尝试将键值对数据从angular应用程序发布到asp.net页面,但无法在asp.net页面接收结果

示例数据

 {fullName: "Vikas Bansal", userId: "1001387693259813", userimageurl: "http://graph.facebook.com/1001387693259813/picture?width=250&height=250"}
角度代码

    var postData = {
        fullName: $scope.name,
        userId: $scope.userId,
        userimageurl: $scope.userImage
    };

    var postUrl = 'http://localhost:41115/Index.aspx';

    $http({
        url: postUrl,
        method: "POST",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: postData
    }).then(function (responce) {
        console.log(responce);
    }, function (responce) {
        console.log(responce);
    });
protected void Page_Load(object sender, EventArgs e)
{
    var userId = Request.Form["userId"];    /// getting null
    var fullName = Request.Form["fullName"];    /// getting null
    var img = Request.Form["userimageurl"]; /// getting null
}
Asp.net页面代码

    var postData = {
        fullName: $scope.name,
        userId: $scope.userId,
        userimageurl: $scope.userImage
    };

    var postUrl = 'http://localhost:41115/Index.aspx';

    $http({
        url: postUrl,
        method: "POST",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: postData
    }).then(function (responce) {
        console.log(responce);
    }, function (responce) {
        console.log(responce);
    });
protected void Page_Load(object sender, EventArgs e)
{
    var userId = Request.Form["userId"];    /// getting null
    var fullName = Request.Form["fullName"];    /// getting null
    var img = Request.Form["userimageurl"]; /// getting null
}
在asp.net页面中,所有变量均为空

编辑

通过将内容类型更改为
'content-type':'application/json;angularJs post请求中的字符集=utf-8'

并使用
var strJSON=(newstreamreader(Request.InputStream.ReadToEnd())我可以得到发布的json

但我仍然想知道是否有一种直接获取键值对的方法


像这样
var userId=Request.Form[“userId”]

这是因为您的负载实际上是
JSON
,但您试图作为
FormData
对象传递

您需要创建一个
FormData()
对象。然后

(1) 告诉AngularJs不要使用
angular.identity
函数对其进行序列化

(2) Angular的PUTPOST请求的默认头是
application/json
,因此您也需要覆盖它。浏览器将
内容类型
设置为
多部分/表单数据
,并填充正确的边界

试试这个:

var postData = new FormData();
postData.append("fullName", $scope.name);
postData.append("userId", $scope.userId);
postData.append("userimageurl", $scope.userImage);

$http.post(postUrl, postData, {
    transformRequest: angular.identity,
    headers: { 'Content-Type': undefined }
}).then(function(data) {
    //success
}, function(error) {
    //error
});

您的内容类型不匹配。您的负载是JSON,但您告诉ASP它是表单。将
内容类型设置为
应用程序/json;charset=utf-8
@kkk使用了这个,但没有收到任何我需要的值。我请求您提供有关
transformRequest:angular.identity,
的一些详细信息,以及您为什么设置
“内容类型”:未定义。非常感谢