未捕获的类型错误:this.$.AjaxPost.go不是一个以函数为核心的ajax

Uncaught TypeError: this.$.AjaxPost.go is not a function - core-ajax

本文关键字:一个以 函数 ajax 核心 go 类型 错误 this AjaxPost      更新时间:2023-09-26

我为我们的网站编写了一个用于密码重置的HTML页面,我们希望它是对服务器的POST调用。我们使用的是聚合物,代码:

<dom-module id="user-password-reset">
<template>
    <div class="popup">
        <h5 class="popup-heading">Forgot Password</h5>
        <div class="popup-body">
            <div class="form-group">
                <label for="FormInput-UserEmail">Provide your Email Address</label>
                <input type="email" class="form-control" id="FormInput-UserEmail" placeholder="name@example.com" value="{{ email::input }}">
            </div>
            <br/>
            <div class="text-center">
                <button type="button" class="btn" on-click="onSubmit">Reset Password</button>
            </div>
        </div>
    </div>
    <core-ajax
        id="AjaxPost"
        auto="false"
        url="/api/user/email"
        method="POST"
        content-type="application/json"
        handle-as="json"
        on-core-response= _handleAjaxPostResponse
        on-core-error= _handleAjaxPostError
        ></core-ajax>
</template>

<script>
    Polymer({
        is: 'user-password-reset',
        behaviors: [
            Polymer.IronOverlayBehavior
        ],
        properties: {
            email: { type: String },
        },
        onSubmit: function( event ) {
            this.$.AjaxPost.params = JSON.stringify( { email: this.email } );
            console.log( this.$.AjaxPost );
            this.$.AjaxPost.go();
        },
        _handleAjaxPostResponse: function( event ) {
            /* Do Something */
        },
        _handleAjaxPostError: function( event ) {
            /* Do Something */      
        },
    });
</script>
</dom-module>

控制台中:

<core-ajax id="AjaxPost" auto="false" url="http://localhost:8080/api/user/email" method="POST" content-type="application/json" handle-as="json" class="style-scope user-password-reset"></core-ajax>

有一个错误说:

Uncaught TypeError: this.$.AjaxPost.go is not a function

我现在该怎么办?

对不起,我忘了提到聚合物的版本。我们使用Polymer 1.0,感谢Ben Thomas指出这一点。解决方案很简单,不需要将其转换为Json,也不需要使用iron ajax来代替核心ajax。

代码:

<iron-ajax
        id="AjaxPost"
        url="/api/user/email"
        method="POST"
        content-type="application/json"
        handle-as="json"
        on-response="_handleAjaxPostResponse"
        on-error="_handleAjaxPostError"
        ></iron-ajax>
var ajaxPost = this.$.AjaxPost;
            ajaxPost.params = { email: this.email };
            ajaxPost.generateRequest();

请注意,如果您使用Post调用,那么最好以params形式发送数据,而不是以body形式发送数据(在某处读取)。发送时,不要将其字符串化。

有时我已经遇到了这个问题。

请尝试使用noConflict(),因为jquery可能与您正在运行的WHAT冲突。

此页面帮助了我:https://api.jquery.com/jquery.noconflict/