【问题标题】:Polymer - Prolems successfully sending core-ajax paramsPolymer - Prolems 成功发送 core-ajax 参数
【发布时间】:2015-02-17 19:05:31
【问题描述】:

我正在尝试获取一些纸质单选按钮的值,然后使用核心 ajax 将它们传递给 URL。我很难知道我是否正确获取了值以及是否将它们传递给 URL。

当我按下按钮时:

<paper-button affirmative hover on-tap="{{addNewGraph}}">Submit</paper-button>

我调用以下脚本:

<script>
    Polymer("add-graphItem",{
        addNewGraph: function () {
            var HeaderName = this.$.graphOptionsLoad.$.headerValue.selectedItem.label;
            var FunctionName = this.$.graphFunctionsLoad.$.functionValue.selectedItem.label;
            console.log("The options are " +HeaderName +" and " +FunctionName);
            this.$.sendOptions.go();
            console.log(sendOptions);
        },
    })
</script>

要使用我正在使用的 core-ajax:

<core-ajax auto url="/getGraph" method="POST" id="sendOptions"></core-ajax>

console.log(sendOptions); 向我抛出“Uncaught ReferenceError: sendOptions is not defined”

想知道我做错了什么,如果有人有任何建议 - 谢谢

这是一个 plunker (http://plnkr.co/edit/WPN3vG8LaKjuWyc0omrp?p=preview),它或多或少地复制了我正在尝试做的事情

【问题讨论】:

  • core-ajax 元素不是 add-graphItem 的属性。您应该将 core-ajax 放入其中或使用 document.querySelector('#sendOptions') 来获取它
  • 也许我遗漏了一些东西,但 add-graphItem 中的 core-ajax 不是(在 plunkr 的第 63 行?)
  • 哦,我明白了,对不起。但是,在从两个列表中选择选项后,Plunker 中的一切都运行良好。
  • 但是我如何检查它发送的内容以及它是否正确 - 我不确定我是否正在获取参数并将它们发送到 core-ajax 以及 core-ajax 是否正在发送这些到 URL(我认为它正在向 URL 发送一些东西,但不认为是参数)
  • 在下面查看我的完整答案

标签: javascript ajax polymer


【解决方案1】:

要确保ajax 是真正的帖子,您应该在服务器端编写目标脚本,它可以简单地回复传入的请求。然后你应该在 core-response 中添加事件监听器,详见https://www.polymer-project.org/docs/elements/core-elements.html#core-ajax

Plunker 代码的传出请求中没有参数定义。可以这样做:

Polymer("add-graphItem", {
  addNewGraph: function () {
    var params = {};
    if (this.$.graphOptionsLoad.$.headerValue.selectedItem) {
      params['HeaderName'] = this.$.graphOptionsLoad.$.headerValue.selectedItem.label;
    }
    if (this.$.graphFunctionsLoad.$.functionValue.selectedItem) {
      params['FunctionName'] = this.$.graphFunctionsLoad.$.functionValue.selectedItem.label;
    }
    this.$.sendOptions.params = JSON.stringify(params);
    this.$.sendOptions.go();
    console.log(params);
  },
});

【讨论】:

  • 这似乎奏效了——仍然需要在服务器端编写一个目标脚本来测试它——但感谢你和我一起完成它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 2018-08-05
  • 1970-01-01
  • 2011-02-23
相关资源
最近更新 更多