0

I dont even know where is wrong code in my ajax, i learn from the internet but still i cant POST data. My html code :

<h3>Input new data</h3>
<form name="contact">
    <input type="text" placeholder="id berita" id="idberita" />
    <input type="text" placeholder="title berita" id="titleberita" />
    <input type="text" placeholder="content berita" id="contentberita" />
    <input id="create_at" type="datetime-local">
    <button type="submit" id="add-data">add</button>
</form>

This is my ajax code :

$('#add-data').on('click', function() {
    var order = {
        id        : $idberita.val(),
        title     : $titleberita.val(),
        content   : $contentberita.val(),
        create_at : $create_at.val()
    };
    $.ajax({
        type   : 'POST',
        url    : 'json/student.json',
        data   : order,
        success: function(newContent) {
            $orders.append('<li>dataid: '+newData.id+', title:'+newData.title+', content:'+newData.content+', create_at: '+newData.create_at+'</li>');
        },
        error: function() {
            alert('error saving data');
        }
    });
});
8
  • 1
    the page reloads because form submits? Commented Aug 29, 2017 at 3:59
  • 1
    Use newContent instead of newData! after success Commented Aug 29, 2017 at 4:02
  • the page reloads,data cant input and i look at the console not show anything.. @guradio Commented Aug 29, 2017 at 4:04
  • what's your eerror? Commented Aug 29, 2017 at 4:05
  • because the page already reloaded Commented Aug 29, 2017 at 4:07

2 Answers 2

1

By default, submitting a form refreshes the page. Jquery's event object is always passed as the first argument of your event handler, so use it to prevent this behavior:

       $('#add-data').on('click', function(event) {
             event.preventDefault();

              var order = {
                id: $('#idberita').val(),
                title: $('#titleberita').val(),
                content: $('#contentberita').val(),
                create_at: $('#create_at').val(),
            };


            $.ajax({
                type: 'POST',
                url: 'json/student.json',
                data: order,
                success: function(newContent) {     
                    $orders.append('<li>dataid: '+newContent.id+', title:'+newContent.title+', content:'+newData.content+', create_at: '+newData.create_at+'</li>');
                },
                error: function(){
                    alert('error saving data');
                }
            });

        });
Sign up to request clarification or add additional context in comments.

4 Comments

i tried you suggestion ang then page not reloads, but when input the data and i look at the console showing "Failed to load resource: the server responded with a status of 405 (Method Not Allowed)"
Which browser are you using?
You may also want to add contentType: "application/json; charset=utf-8", dataType: "json", cache: false options to your ajax call.
i know, i cant input from the local json file
0

use .serialize() it will collecting all data input element within form into a set of array, you dont need to define by one by

$.ajax({
  type: "POST",
  data: $("#formid").serialize(),
  url : "your/post/url",
  success: function(callback){
     // success handler
  },
  error: function(){
    // error handler
  }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.