从 AngularJS 和 ASP.net MVC 传递数据 $http.post 为空

编程入门 行业动态 更新时间:2024-10-20 07:42:32
本文介绍了从 AngularJS 和 ASP MVC 传递数据 $http.post 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试将数据从 AngularJS 传递到 ASP MVC,但始终为 null.这是我的代码(仅发布必要的按钮、控制器和 c#:

i'm trying to pass data from AngularJS to ASP MVC and is always getting null. Here's my code (only posting the essential, button, controller and c#:

HTML:

<a class="btn btn-grey btn-lg btn-block" ng-click="AddCar()">Save</a>

控制器

$scope.AddCar = function () {
            $http.post("Cars/AddCar", JSON.stringify($scope.new.JsonCar)).success(function (data) {
                Alert(ok)
            })

c#

public string AddCar(string JsonCar) 
        {
            try
           ....
        }

在 JSON.stringify($scope.new.JsonCar) 我得到这个:

In JSON.stringify($scope.new.JsonCar) i'm getting this:

"{"Name":"FIAT 500","Description":"Newcar","MaxUserCapacity":5,"PhotoPath":"none"}"

"{"Name":"FIAT 500","Description":"New car","MaxUserCapacity":5,"PhotoPath":"none"}"

我做错了什么?

推荐答案

直接将对象作为对象传递,而不是将其字符串化.由于它现在正在传递,因此它是一个字符串,而不是可以正确反序列化的对象.

Pass your object directly as an object rather than stringifying it. As it's being passed right now, it's a string, not an object that can properly be deserialized.

$http.post("Cars/AddCar", $scope.new.JsonCar).success(function (data) {
            Alert(ok)
        })

创建一个与您的负载匹配的 Car 对象.序列化程序将为您处理您的 JSON 对象.

Create a Car object that matches your payload. The serializer will handle your JSON object for you.

public Car AddCar(Car car) 
    {
        try
       ....
    }

我的假设是,无论如何,您都会将字符串反序列化为对象.这只是为您节省了额外的步骤.

My assumption is that at some point you are deserializing your string into an object regardless. This just saves you that extra step.

这篇关于从 AngularJS 和 ASP MVC 传递数据 $http.post 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 21:40:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1011314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   数据   net   AngularJS   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!