将json对象传递到外部API时出现问题
首先,我无法控制API是如何构造的,我需要做的就是基本上调用它 现在我需要传入以下内容,如下所示:将json对象传递到外部API时出现问题,json,ajax,api,Json,Ajax,Api,首先,我无法控制API是如何构造的,我需要做的就是基本上调用它 现在我需要传入以下内容,如下所示: { "ConsumerAggregatedAttributes": [ { "ConsumerAggregatedAttributeID": 28, "ConsumerID": "1159040334", "Applicat
{
"ConsumerAggregatedAttributes": [
{
"ConsumerAggregatedAttributeID": 28,
"ConsumerID": "1159040334",
"ApplicationCode": "36363636",
"LocaleCode": null,
"AttributeCode": "FailedLoginAttemps",
"Value": "0",
"CreateTS": "2015-12-14 23:53:30.517",
"UpdateTS": "2015-12-15 01:12:06.947"
}
],
"ConsumerID": "1159040334",
"PersonalQRImageGUID": "a30d62db-0981-4b7c-bed0-4cf66d2fe1a3",
"PictureMediaGUID": null,
"Nickname": "testing",
"LastGPSCoordinate": null,
"TotalCredit": 0,
"AvailableCredit": 0,
"IsActive": true,
"IsPictureApproved": false,
"Firstname": "test",
"Lastname": "test",
"Username": "test.email@test.com.au",
"Password": null,
"Email": "test.email@test.com.au",
"Mobile": "04145588774",
"Address": null,
"City": null,
"Province": null,
"PostalCode": null,
"Country": null,
"DateOfBirth": null,
"Gender": null,
"Language": null,
"CreateTS": "2015-12-14 23:51:27.143",
"UpdateTS": "2015-12-15 01:17:22.567",
"ConsumerTypeCode": "Admin",
"DefaultLocaleCode": "EN",
"IsTestUser": false,
"Distance": null
}
现在,我的ajax调用如下所示:
$http({
method: 'POST', url: 'https://someUrl.com/servicecol.svc/updateProfile?$expand=ConsumerAggregatedAttributes&LocationID=1&Role=Manager&IsProfileComplete=True', headers: {
t: ts, // Time Stamp
vt2: hashInBase64,// Encrypted code
a: ac, // Account Id
l: la, // Local
c: cId // Consumer Id
},
data: {
"ConsumerAggregatedAttributes": [
{
"ConsumerAggregatedAttributeID": 28,
"ConsumerID": "1159040334",
"ApplicationCode": "36363636",
"LocaleCode": null,
"AttributeCode": "FailedLoginAttemps",
"Value": "0",
"CreateTS": "2015-12-14 23:53:30.517",
"UpdateTS": "2015-12-15 01:12:06.947"
}
],
"ConsumerID": "1159040334",
"PersonalQRImageGUID": "a30d62db-0981-4b7c-bed0-4cf66d2fe1a3",
"PictureMediaGUID": null,
"Nickname": "testing",
"LastGPSCoordinate": null,
"TotalCredit": 0,
"AvailableCredit": 0,
"IsActive": true,
"IsPictureApproved": false,
"Firstname": "test",
"Lastname": "test",
"Username": "test.email@test.com.au",
"Password": null,
"Email": "test.email@test.com.au",
"Mobile": "04145588774",
"Address": null,
"City": null,
"Province": null,
"PostalCode": null,
"Country": null,
"DateOfBirth": null,
"Gender": null,
"Language": null,
"CreateTS": "2015-12-14 23:51:27.143",
"UpdateTS": "2015-12-15 01:17:22.567",
"ConsumerTypeCode": "Admin",
"DefaultLocaleCode": "EN",
"IsTestUser": false,
"Distance": null
}
}).success(function (data, status, headers, config) {
$scope.response = data;
}).error(function (data, status, headers, config) {
console.log(data);
$scope.response = data;
});
现在,当我调用这个函数时,它应该像这样传递它:
https://someUrl.com/servicecol.svc/updateProfile?$expand=ConsumerAggregatedAttributes&LocationID=1&Role=Manager&IsProfileComplete=True
{
"ConsumerAggregatedAttributes": [{
"ConsumerAggregatedAttributeID": 28,
"ConsumerID": "1159040334",
"ApplicationCode": "36363636",
"LocaleCode": null,
"AttributeCode": "FailedLoginAttemps",
"Value": "0",
"CreateTS": "2015-12-14 23:53:30.517",
"UpdateTS": "2015-12-15 01:12:06.947"
}],
"ConsumerID": "1159040334",
"PersonalQRImageGUID": "a30d62db-0981-4b7c-bed0-4cf66d2fe1a3",
"PictureMediaGUID": null,
"Nickname": "testing",
"LastGPSCoordinate": null,
"TotalCredit": 0,
"AvailableCredit": 0,
"IsActive": true,
"IsPictureApproved": false,
"Firstname": "test",
"Lastname": "test",
"Username": "test.email@test.com.au",
"Password": null,
"Email": "test.email@test.com.au",
"Mobile": "04145588774",
"Address": null,
"City": null,
"Province": null,
"PostalCode": null,
"Country": null,
"DateOfBirth": null,
"Gender": null,
"Language": null,
"CreateTS": "2015-12-14 23:51:27.143",
"UpdateTS": "2015-12-15 01:17:22.567",
"ConsumerTypeCode": "Admin",
"DefaultLocaleCode": "EN",
"IsTestUser": false,
"Distance": null
}
但它却像这样传递
https://someUrl.com/servicecol.svc/updateProfile?$expand=ConsumerAggregatedAttributes&LocationID=1&Role=Manager&IsProfileComplete=True**[object Object]**
正如您在Url末尾看到的,它有object object,经过我自己和拥有该API的人的调查,这就是它失败的原因,有什么能说明我如何才能让它真正正确地发布,而不是在Url中添加[object object]?post数据不应该附加到Url末尾,而是通过请求主体发送。GET请求的url中发送的数据将有最大大小限制 您试图在上述代码中实现的结果看起来像一个GET:
https://someUrl.com/servicecol.svc/updateProfile?params{JSON}
所有这些JSON数据都可能超过限制(可能是255字节),而且您似乎正在$http方法中使用POST
尝试一下,我已经为我对您的代码所做的更改添加了注释:
工作提琴供参考:
你的问题可能是由很多事情造成的。使用API可能非常困难,因为所有内容都需要按照API所期望的方式发送
确保检查开发人员工具的控制台是否有任何错误
对于我的小提琴,我正在使用一个名为的免费服务,它允许您从服务器的角度查看帖子信息。您还可以使用尝试在浏览器之外调试问题,方法是使用不同的方法将标题和参数传递给API。您使用的是AngularJS吗?嘿。感谢您的回复,是的,我很感谢您的帮助,我同意使用API是一件痛苦的事情,尤其是当您无法控制它们时,并且提供的文档是有限的。
// Stringify the JSON
var postData = JSON.stringify({
"ConsumerAggregatedAttributes": [{
"ConsumerAggregatedAttributeID": 28,
"ConsumerID": "1159040334",
"ApplicationCode": "36363636",
"LocaleCode": null,
"AttributeCode": "FailedLoginAttemps",
"Value": "0",
"CreateTS": "2015-12-14 23:53:30.517",
"UpdateTS": "2015-12-15 01:12:06.947"
}],
"ConsumerID": "1159040334",
"PersonalQRImageGUID": "a30d62db-0981-4b7c-bed0-4cf66d2fe1a3",
"PictureMediaGUID": null,
"Nickname": "testing",
"LastGPSCoordinate": null,
"TotalCredit": 0,
"AvailableCredit": 0,
"IsActive": true,
"IsPictureApproved": false,
"Firstname": "test",
"Lastname": "test",
"Username": "test.email@test.com.au",
"Password": null,
"Email": "test.email@test.com.au",
"Mobile": "04145588774",
"Address": null,
"City": null,
"Province": null,
"PostalCode": null,
"Country": null,
"DateOfBirth": null,
"Gender": null,
"Language": null,
"CreateTS": "2015-12-14 23:51:27.143",
"UpdateTS": "2015-12-15 01:17:22.567",
"ConsumerTypeCode": "Admin",
"DefaultLocaleCode": "EN",
"IsTestUser": false,
"Distance": null
});
// Fill these out with your private data
var ts = new Date().getTime();
var vt2 = "dGVzdA==";
var ac = 123;
var la = "en";
var cId = 321;
// Made the keys strings
var postHeaders = {
"t": ts, // Time Stamp
"vt2": hashInBase64,// Encrypted code
"a": ac, // Account Id
"l": la, // Local
"c": cId // Consumer Id
}
// Moved the params off of the url into an object
var postParams = {
"$expand": "ConsumerAggregatedAttributes",
"LocationID": 1,
"Role": "Manager",
"IsProfileComplete": "True"
}
// Added the params. Simplified by extracting data into external variables.
var req = {
method: "POST",
url: "https://someUrl.com/servicecol.svc/updateProfile",
headers: postHeaders,
params: postParams,
data: postData
}
// The $http legacy promise methods success and error have been deprecated. Use the standard then method instead.
$http(req).then(function successCallback(response) {
$scope.response = response;
}, function errorCallback(response) {
$scope.response = response;
});