简体   繁体   English

AJAX帖子未保存,是否在没有任何控制台日志记录的情况下刷新页面?

[英]AJAX post not saving, and is refreshing the page without any console logging?

With the form, the submit button refreshes, has yet to save, and is not logging anything for some reason.. 使用该表单,提交按钮将刷新,尚未保存,并且由于某种原因未记录任何内容。

..though the same style AJAX post is working in another app, with almost identical HTML.. ..尽管同一个样式的AJAX帖子可以在另一个应用中使用几乎相同的HTML。

There have been recommendations to remove the form tags, and change the submit button to input type="button"...though with this the page literally does nothing 建议删除表单标签,并将提交按钮更改为输入type =“ button” ...尽管如此,该页面实际上不执行任何操作

The API (NodeJS) is working properly, as tested with Postman.. 经Postman测试,API(NodeJS)正常运行。

Here's the HTML (am using handelbars templating, both server-side and client-side): 这是HTML(在服务器端和客户端都使用手把模板进行模版制作):

<div id="allStories" class="allStories"> </div><!--/allStories-->
<script id="storyTemplate" type="text/x-handlebars-template">

  <div class="thisness">
      <div class="stories">

          \{{#each stories}}
              <input type="text" class="formFormat" value="\{{ story }}"/>
          \{{/each}}

          <form id="newStoryForm">
              <input type="text" id="newStory" placeholder="…" />

            <br><button type="submit" class="btn" id="AddStory">add</button>

          </form> <!--/newStoryForm-->

      </div> <!--/stories-->
    </div> <!--/thisness-->

</script>

And Here's the AJAX: 这是AJAX:

$(document).ready(function(){

    // GET ALL
    // * * * * * * * * * * * * * * * * * * * * * * * * * * *

    function foundAllSuccess(resJSON) {

        var templateSource = $("#storyTemplate").html();

        console.log('Getting All the stories..');
        console.log(templateSource);

        var template = Handlebars.compile(templateSource);

        var storyHTML = template({stories: resJSON});

        $('#allStories').html(storyHTML);

        console.log('All is Proper');

    }

    function foundAllFailure() {
        alert(textStatus + "**:**" + errorThrown);
    }

    function refreshStories(){

        $.ajax({
            url:"http://localhost:4200/api/v1/stories",
            method:'get',
            success: foundAllSuccess,
            error: foundAllFailure

        })

    }

    refreshStories(); // gets called when the page loads


    // POST
    // * * * * * * * * * * * * * * * * * * * * * * * * * * *

    // AddStory button clicks, adding new story    
    $( "#AddStory" ).click(function() {

        // story element for API
        var story = $( "#newStory" ).val();    
        console.log(story);

        var AjaxPostData = {
            story : story
        };

        // if the story field has content
        if (story.length != 0) {
          console.log('there is a story: ' + story);
            // make an ajax call
            $.ajax({
            dataType: 'json',
            data: AjaxPostData,
            type: 'post',
                url:"http://localhost:4200/api/v1/stories",
                success: refreshStories,
                error: foundAllFailure
            });
        };

        console.log(AjaxPostData.story);

    });

}); // doc is ready

I am not able to comment. 我无法发表评论。 So, i am proceeding with assumption. 因此,我正在进行假设。 There are three things that i want to point out. 我想指出三件事。

One, click handler should be updated to submit handler on the "form". 一,单击处理程序应进行更新以在“表单”上提交处理程序。 Also, please do not remove "form" tag and "button type="submit". As these tags help in screen reader software and auto fill functionality of browsers. 另外,请不要删除“ form”标签和“ button type =“ submit”,因为这些标签有助于屏幕阅读器软件和浏览器的自动填充功能。

Second, click handler is bound in ready event, at that time this button is not present inside DOM. 其次,单击处理程序绑定在ready事件中,那时该按钮在DOM中不存在。 So, we have two options here, either put click handler (change it to submit handler, as submit button fires both event, first it fires submit event and then click event) inside the foundAllSuccess at the last line or use jQuery "on/live" method to attach submit handler. 因此,我们在此处有两个选择,要么在最后一行的foundAllSuccess内部放置click处理程序(将其更改为submit handler,因为submit按钮同时触发这两个事件,首先它触发submit事件,然后单击事件),或者在活动/实时使用jQuery ”附加提交处理程序的方法。

Third, in the click/submit handler, do event.preventDefault() and at last line do return false. 第三,在点击/提交处理程序中,执行event.preventDefault()并在最后一行返回false。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM