无法访问脚本部分中的iron-ajax对象

时间:2015-11-30 20:09:12

标签: html ajax polymer polymer-1.0

我有以下代码。 chrome控制台说:

  

未捕获的TypeError:无法读取属性' ajax'未定义的

我通过凉亭安装了铁-ajax,平台,聚合物,promise-polyfill和webcomponentsjs。

我从未用聚合物编程。 什么可能是一个错误?什么进口?

  

主mind.html

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

<dom‐module id="master-mind">
    <style>


    </style>

    <template>
        <iron-ajax
            auto
            id = "ajax"
            handle-as = "json"
            method = "get"
            last-response="{{createCodeResponse}}"
            debounce-duration="300">
        </iron-ajax>
        <textarea rows="1" id="actual_code" style="width: 490px;" readonly="readonly" value="{{createCodeResponse}}"></textarea>
        <br />
        <textarea rows="10" id="messages" style="width: 490px;" readonly="readonly"></textarea>
        <br />
        <input type="text" id="message" size="60" />
        <input value="Send" onclick="check();" type="button" /> 
    </template> 

    <script>
        Polymer({
            is : "master-mind",

            ready: function() {
                this.$.ajax.url = "http://localhost:8080/KSWE_7_1/resources/mastermind/";
            }

        });
    </script> 

</dom‐module>
  

的index.html

<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>MasterMind</title>
<script src='js/jquery.js'></script>
<script src='bower_components/webcomponentsjs/webcomponents.min.js'></script>
<link rel='import' href='master-mind.html' />
</head>

<body>
    <br />
    <master-mind></master-mind>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试在附加方法中异步执行,如下所示:

attached: function(){
  this.async(function(){
    this.$.ajax.url = "http://localhost:8080/KSWE_7_1/resources/mastermind/";
  },someTimeIfThereAreManyItemsToLoad);
}

修改

我不知道您的错误在哪里,但此代码有效:

 <dom-module id="master-mind">
    <template>
      <iron-ajax
            auto
            id = "ajax"
            handle-as = "json"
            method = "get"
            last-response="{{createCodeResponse}}"
            debounce-duration="300"></iron-ajax>
    </template>
    <script>
        Polymer({
            is: "master-mind",  
            ready:function(){
              this.$.ajax.url = "http://localhost:8080/KSWE_7_1/resources/mastermind/";
              console.log(this.$.ajax.url);
            },
        });
    </script>
</dom-module>