0

我正在开发一个聚合物应用程序,我想调用 RestApi。这就是请求正文的样子

{
    "languageId": Presently English is the only supported language. Always 1, 
    "productCode":"Medicus",
    "timeZoneName":"Time zone name of device. For e.g. Asia/Calcutta",
    "timeZoneOffset": Time zone offset from UTC in milliseconds. For e.g. IST = 19800000,
    "user":{
     "firstName":"First name of the user",
     "lastName":"Last name of the user",
     "middleName":"Middle name of the user",
     "password":"Password provided by the user",
     "userTypeId":2 = Doctor, 3 = User,
     "fields":[
         {
            "Id":1,
            "values":["Mobile number provided by the user”]
         }
     ]
    }
}

我没有正确理解应该如何在 iron-ajax 元素的 params='{}' 中指定这些参数。

4

1 回答 1

0

在您的模板中放入类似的内容(我假设 POST 到您的其余 API,因为您body在问题中说过。如果它的 GET 替换body=params=

<iron-ajax
       id="fetchday"
       url="/api/fetchday"
       handle-as="json"
       content-type="application/json"
       method="POST"
       body="[[params]]"
       last-response="{{results}}"
       on-response="_gotData"
       on-error="_error"></iron-ajax>

并在您的聚合物元素属性中

Polymer({
  is: 'my-element'
  properties: {
    params: {
     type: Object
    } 
  },
  _someFunction: function() {
    this.params = //ASSIGN YOUR JSON OBJECT TO PARAMS HERE
    this.$.fetchday.generateRequest();
  },
  _gotData: function(e) {
    //response data is in both this.results and e.detail.response
 },
 _error: function() {
 }
});
于 2016-10-20T09:09:46.337 回答