将json对象传递给外部API的问题

时间:2015-12-19 01:26:55

标签: json ajax api

首先,我无法控制API的构建方式,我需要做的就是基本上调用它。

现在我需要传递以下内容:

{
            "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的末尾所看到的,它有对象对象,经过我自己和拥有这个API的人的调查,这就是它失败的原因,可以说明我如何得到它实际上是正确发布而不是在网址中有[对象]?

1 个答案:

答案 0 :(得分:0)

POST数据不应附加到网址的末尾,而是通过请求正文发送。在GET请求的URL中发送的数据将有最大大小限制。

您在上面的代码中尝试实现的结果类似于GET:

https://someUrl.com/servicecol.svc/updateProfile?params{JSON}

所有JSON数据都可能超出限制(可能是255个字节),而且看起来你在$ http方法中使用了POST。

尝试一下,我已经为我对您的代码所做的更改添加了评论:

工作小提琴以供参考:http://jsfiddle.net/jqn5pd9x/1/

// 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;
});

您的问题可能是由于许多原因造成的。 API可能非常难以使用,因为所有内容都需要准确地发送API的期望。

请确保您检查开发人员工具控制台是否有任何错误。

对于我的小提琴我使用的是名为Henry's HTTP Post Dumping Server的免费服务,它允许您从服务器的角度查看您的帖子信息。您还可以使用postman尝试在浏览器之外调试您的问题,方法是使用不同的方法将标头和参数传递给API。