将Json数据从Angularjs发布到MVC控制器
当我将JSON数据从AngularJS传递到MVC时。我正在犯错误 Http请求配置url必须是字符串或$sce受信任对象。收到:{“方法”:“POST”,“url”:“Home/SavePDDetails”,“数据类型”:“json”,“数据”:{“PD”:{“名称”:“qqq”,“地址”:“www”}}} MVC代码: AngularJS代码将Json数据从Angularjs发布到MVC控制器,angularjs,json,asp.net-mvc-5,Angularjs,Json,Asp.net Mvc 5,当我将JSON数据从AngularJS传递到MVC时。我正在犯错误 Http请求配置url必须是字符串或$sce受信任对象。收到:{“方法”:“POST”,“url”:“Home/SavePDDetails”,“数据类型”:“json”,“数据”:{“PD”:{“名称”:“qqq”,“地址”:“www”}}} MVC代码: AngularJS代码 在您的函数中尝试以下操作 使用JSON.stringify()包装JSON 如果数据和url作为config对象的属性传递,请不要使用$http.po
在您的函数中尝试以下操作 使用JSON.stringify()包装JSON
如果数据和url作为
config
对象的属性传递,请不要使用$http.post
方法。只需使用$http
:
̶$̶h̶t̶t̶p̶.̶p̶o̶s̶t̶(̶{̶
$http({
method: 'POST',
url: 'Home/SavePDDetails',
̶d̶a̶t̶a̶t̶y̶p̶e̶:̶ ̶"̶j̶s̶o̶n̶"̶,̶
data: {
PD: $scope.PD
}
})
无需对数据进行字符串化,因为会自动执行该操作。您可以控制台$scope.PD对象并添加问题吗?
.success
方法已被禁用。请参阅。
$scope.Click = function() {
console.log('clicked');
$http.post({
method: 'POST',
url: 'Home/SavePDDetails',
datatype: "json",
data: {
PD: $scope.PD
}
}).success(function(response) {
console.log('success');
console.log(response);
}).error(function(response) {
console.log('error');
console.log(response);
});
}
var parameter = JSON.stringify({PD: $scope.PD});
$http.post('Home/SavePDDetails', parameter).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log(data);
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
̶$̶h̶t̶t̶p̶.̶p̶o̶s̶t̶(̶{̶
$http({
method: 'POST',
url: 'Home/SavePDDetails',
̶d̶a̶t̶a̶t̶y̶p̶e̶:̶ ̶"̶j̶s̶o̶n̶"̶,̶
data: {
PD: $scope.PD
}
})