gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:29:19 26 4
gpt4 key购买 nike

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

示例数据

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

Angular 代码

    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);
});

Asp.net 页面代码

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 页面中所有变量都是空的。

enter image description here

编辑

通过将内容类型更改为 'Content-Type': 'application/json; charset=utf-8' 在 angularJs post 请求中

并通过使用 var strJSON = (new StreamReader(Request.InputStream).ReadToEnd()); 我可以获得发布的 json

但我还是想知道有没有办法直接获取键值对?

像这样 var userId = Request.Form["userId"];

最佳答案

这是因为您的有效载荷实际上是 JSON 但您试图作为 FormData 对象传递。

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

(1) 使用angular.identity 函数告诉AngularJs 不要序列化它。

(2) Angular 的 PUTPOST 请求的默认 header 是 application/json,因此您也需要覆盖它。浏览器将 Content-Type 设置为 multipart/form-data 并填充正确的边界。

试试这个:

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
});

关于javascript - AngularJS 将数据作为键值对发布到 asp.net 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com