angularjs - 将 Json 数据从 Angularjs 发布到 MVC 控制器
问题描述
当我将 JSON 数据从 AngularJS 传递到 MVC 时。我得到以下错误。
Http 请求配置 url 必须是字符串或 $sce 可信对象。收到:{"method":"POST","url":"Home/SavePDDetails","datatype":"json","data":{"PD":{"Name":"qqq","Address" :“万维网”}}}
MVC 代码:
[HttpPost]
public JsonResult SavePDDetails(PDDetailsDTO PD)
{
new PDDetailsDAL().SavePDDetails(PD);
return Json(new { Success = true, Message = "Success" });
}
AngularJS 代码
$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);
});
}
解决方案
If data and url are passed as a properties of the config
object, don't use the $http.post
method. Simply use $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
}
})
There is no need to stringify the data as the $http Service does that automatically.
推荐阅读
- reactjs - 如何使用固定标题使 Material UI 表水平和垂直滚动
- python - SQLITE - 无法将数千个删除语句组合成单个事务
- typescript - TypeScripts “从不输入对 JSDoc 不可见?
- ruby-on-rails - 无法打开到 oauth.intuit.com:443 的 TCP 连接(getaddrinfo:名称或服务未知)
- python - celery 不能自定义属性
- android - Android Studio - 仅导入启动器资产更新 ic_launcher-playstore.png 和 mipmap 文件夹下的启动器图标被删除
- node.js - 如何从 NodeJS 服务器提供大型 GIF 文件?
- javascript - 当我尝试使用 JavaScript 客户端连接到 Elasticsearch 时,为什么会出现错误“无法读取未定义的属性 'MAX_LENGTH'”?
- average - 松树脚本不匹配的输入''期待'行尾没有续行'
- node.js - 如何将大量数据从 node.js 应用程序发送到 python 文件?