$ http.post要求不angularJS工作吗? [英] $http.post request not working in angularJS?

查看:145
本文介绍了$ http.post要求不angularJS工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经回答了这个网站severeal时间,但他们都不帮助。于是我又问:

This question has been answered severeal time on this site but none of them are helping. So I ask again:

当我做出这样的POST请求:

When I make a POST request like this :

var sectionTypeVM = new Object();
        sectionTypeVM.SectionTypeId = 0;
        sectionTypeVM.Name = $scope.message;
        $http({
            method: 'POST',
            url: 'http://mtapi.azurewebsites.net/api/sectiontype',
            dataType: 'json',
            data: sectionTypeVM,
            headers: { 'Content-Type': 'application/json; charset=UTF-8' }
        }).success(function (data) {
            alert(data);
        }).error(function (data) {
            alert(data);
        });

这工作完全正常,但是当我尝试做这样的事情:

it works perfectly fine, but when I try to do something like this:

        $http({
            method: 'POST',
            url: 'http://mtapi.azurewebsites.net/api/sectiontype',
            dataType: 'json',
            data: $scope.message,
            headers: { 'Content-Type': 'application/json; charset=UTF-8' }
        }).success(function (data) {
            alert(data);
        }).error(function (data) {
            alert(data);
        });

它没有。为什么我需要创建一个单独的对象的JavaScript的方式发送,为什么不能直接贴我的angularJS对象(它们看起来一样)?它是一个服务器端的错误还是什么? A解释会有所帮助。

it doesn't. Why I need to create a separate object the javascript way and send it, why my angularJS object cannot be posted directly (they look same ) ? Is it a server side error or what ? A explanation will be helpful.

推荐答案

的2柱间的主要区别在于,在第​​一发回一个对象具有两个字段(名称和SectionTypeId),而第二只发送回的内容$ scope.message。 我可能是错的,但它看起来像$ scope.message是一个字符串。但是,您的内容类型设置为应用程序/ JSON。

The main difference between your 2 posts is that the first sends back an object with two fields (Name and SectionTypeId), while the second only sends back the contents of $scope.message. I could be wrong, but it looks like $scope.message is a string. However, you are setting the content-type to application/json.

这两者之间的区别是第一篇文章发送此的对象的:

The difference between the two is the first post sends this object:

{
    SectionTypeId: 0,
    Name: [
        {"name"="abc", "id"="1"},
        {"name"="bcd", "id"="2"}
    ]
}

而第二个职位发送此的阵列的:

[
    {"name"="abc", "id"="1"},
    {"name"="bcd", "id"="2"}
]

您需要或者重组你的code使得$ scope.message是你的服务器接受或喜欢你的第一个例子中的对象包装$ scope.message有效的JSON对象。

You need to either restructure your code so that $scope.message is a valid json object that your server accepts or wrap $scope.message in an object like your first example.

这篇关于$ http.post要求不angularJS工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆