【发布时间】:2026-02-24 11:35:02
【问题描述】:
这是我的服务:
home.factory("homeService", function ($http, $q) {
var service =
{
getAssets: function () {
var deferred = $q.defer();
var response = $http({
method: "post",
dataType: "json",
data: '',
headers: {
'Content-Type': "application/json"
},
url: "http://localhost/myWeb/services/reports_ws.asmx/getData",
});
response.success(function (data) {
deferred.resolve(data);
});
response.error(function (data) {
alert('Error');
});
// Return the promise to the controller
return deferred.promise;
},
}
return service;
当我使用 application/json 作为内容时,我从服务器收到 500 错误。使用纯文本可以正常工作并返回数据,但是虽然服务器以 json 格式发回数据,但仍以 xml 格式返回。我在 Chrome 中测试过,一切正常。我还注意到 Chrome 使用“application/x-www-form-urlencoded”作为内容类型发送请求。我也试过了,但仍然有xml中的数据。请帮忙。
谢谢
【问题讨论】:
-
请检查浏览器开发者工具中的网络标签。如果它显示 XML 则服务器发送 XML ......这看起来很简单。也许您只认为服务器发送 JSON :-) 使用 $http 的
headers选项只会更改请求标头,而不是响应标头 -
问题出在服务器端。您是如何更改 chrome 中的内容类型的?我认为您发送了一个表格。因此数据作为输入而不是作为 json 对象发送。服务方法不理解json格式。