这是我的第一个问题,我真的需要帮助。
我试图使用 post 方法将大规模 JSON 字符串从我的 conroller 发送到通用处理程序$http.但是当控件命中http方法时,控件丢失而没有给出任何错误,我尽力解决,因为我是角度js的初学者,任何人都可以帮助我解决这个问题....
$http({
method: 'POST',
url: 'FinjHandler.ashx',
params: { SaveJournal: JSON.stringify($scope.ViewData.FilterData) },
}).success(function (RetData) {
alert('Saved');
});
对于 POST 请求,应使用配置对象的 data
属性指定数据:
$http({
method: 'POST',
url: 'FinjHandler.ashx',
//contenttype: '',
//params: { SaveJournal: JSON.stringify($scope.ViewData) },
//USE data property
data: $scope.ViewData,
/*
headers: {
'Content-Type': 'text / plain',
'Content-Length': $scope.paramsLength
},*/
}).then(function onSuccess(response) {
// Handle success
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
//...
alert('saved');
}, function onError(response) {
// Handle error
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
//...
});
使用 params
属性,框架对数据进行 URL 编码,并将其作为搜索参数追加到 URL。POST 数据通常不使用数据的 URL,因为百分比编码效率非常低。
AngularJS框架会自动将JavaScript对象或数组编码为JSON格式的UTF-8字符串。它使用application/json
作为默认内容类型。
对于二进制数据,请将Content-Type
标头设置为 undefined
:
//FOR FormData, files, and blobs
headers: { 'Content-Type': undefined }
通过将内容类型设置为 undefined,AngularJS 框架将省略默认设置 application/json
,XHR API 将适当设置内容类型。