【问题标题】:Issue passing in json object to external API将 json 对象传递给外部 API 的问题
【发布时间】:2015-12-19 01:26:54
【问题描述】:

首先我无法控制 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 的人的调查后,这就是它失败的原因,任何人都可以阐明我如何获得它实际上正确发布而不是在 url 中有 [object object]?

【问题讨论】:

  • 你在使用 AngularJS 吗?
  • 嘿。感谢您的回复,是的,我是

标签: json ajax api


【解决方案1】:

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

您试图在上面的代码中实现的结果看起来像一个 GET:

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

所有 JSON 数据都可能超过限制(可能是 255 个字节),而且您似乎在 $http 方法中使用了 POST。

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

工作小提琴供参考: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。

【讨论】:

  • 感谢您的帮助,我同意 API 使用起来很痛苦,尤其是当您无法控制它们时,并且提供的文档有些有限。
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 2021-01-21
  • 2015-09-01
  • 2017-10-18
相关资源
最近更新 更多