gpt4 book ai didi

mysql - 如何在angularjs中的restful api中传递数据

转载 作者:行者123 更新时间:2023-11-29 21:24:15 25 4
gpt4 key购买 nike

我想在 mysql 表中添加一个新条目。我从一个高级的 Restful 客户端测试了 API。运行良好。

但是当我尝试使用 Angular js 时,没有添加条目。

服务代码:

sampleApp.factory('Brand', function ($resource) {
return $resource('http://<mydomain>/api/v1/brands/:id');
});

Controller 代码:

$scope.brand = new Brand();
$scope.brand.id = parseInt($scope.Brands[$scope.Brands.length-1].id, 10) + 1;
$scope.brand.name = $filter('toTitleCase')($scope.NewBrand.name);
$scope.brand.image = '/images/brands/' + $filter('toLowerCase')($scope.NewBrand.name) + '.png';


Brand.save($scope.brand, function() {
alert('data saved');
//data saved. do something here.
});

这里出了什么问题?有人可以帮忙吗?

最佳答案

要转换为以表单编码形式发送而不是默认的 application/json,以下示例是从 $httpParamSerializerJQLike 的 Angular 文档复制的。

.controller(function($http, $httpParamSerializerJQLike) {
//...

$http({
url: myUrl,
method: 'POST',
data: $httpParamSerializerJQLike(myData),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});

});

您还可以在应用程序的配置阶段为所有 $http 设置全局默认值

还可以在 $http 文档中查找将序列化器作为字符串名称传递的替代方法

如果您使用 jQuery,序列化程序与 $.param() 相同

关于mysql - 如何在angularjs中的restful api中传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35583233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com