Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Polymer 1.x: Using iron-ajax inside a custom behavior

I'm building a custom behavior. Call it MyBehaviors.MySpecialBehavior.

But I need to get data that's stored locally in a JSON file called my-data.json.

How can I do this inside my behavior? I'm trying to import iron-ajax but I can't think of how to access its methods or properties.

my-special-behavior.html
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">

<script>
  var MyBehaviors = MyBehaviors || {};
  MyBehaviors.MySpecialBehaviorImpl = {
    // Methods go here that rely on data at my-data.json
  };

  MyBehaviors.MySpecialBehavior = [
    MyBehaviors.MySpecialBehaviorImpl,
  ];
</script>
my-data.json
{
  "important": "data",
  "j": 5,
  "o": "N",
  "goes": "here"
}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can create elements programatically. Have a look at how iron-ajax itself does that to use iron-request internally:

https://github.com/PolymerElements/iron-ajax/blob/master/iron-ajax.html#L442

Refering to your usecase, the user a1626 created this snippet:

var ajax = document.createElement('iron-ajax');
ajax.contentType = "application/json";
ajax.handleAs = "json";
ajax.url = <url goes here>
ajax.method = 'get';
ajax.addEventListener('response', function (event) {
    //response handler                  
});
ajax.generateRequest();
about 4 years ago · Santiago Trujillo Report

0

You can access the json data with ajax.lastResponse inside the added event listener.

var ajax = document.createElement('iron-ajax');
ajax.contentType = "application/json";
ajax.handleAs = "json";
ajax.url = <url goes here>
ajax.method = 'get';
ajax.addEventListener('response', function (event) {
    //response handler
    console.log('ajax', ajax.lastResponse);            
});
ajax.generateRequest();
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!