Javascript AngularJS将数据作为键值对发布到asp.net页面
我正在尝试将键值对数据从angular应用程序发布到asp.net页面,但无法在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
{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的PUT和POST请求的默认头是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,
的一些详细信息,以及您为什么设置“内容类型”:未定义。非常感谢