click jquery button + send data without form - bookmark

前端 未结 3 960
太阳男子
太阳男子 2021-01-17 22:13

I\'m working on a bookmarking function where the user clicks on a jQueryui button and certain information is sent to the database. But I\'m not using a form, because there i

相关标签:
3条回答
  • 2021-01-17 22:57

    Instead of using .ajax() use either .get() or .post()

    Using .get()

     $.get('controller/addBookmark',function(data){ 
         alert('Your bookmark has been saved'); 
     });
    

    Using .post()

     $.post('controller/addBookmark', function(data) {
         alert('Your bookmark has been saved, The contents of the page were:' + data); 
     });
    
    0 讨论(0)
  • 2021-01-17 22:57

    from the documentation of jQuery.ajax()

    Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.

    If you do not know what to put for the data, probably you should remove that option? In your controller function addBookmark(), you can reduce the code by remove the if check. Try this and see if it works for you.

    0 讨论(0)
  • 2021-01-17 23:06

    Your problem is you are checking for a submit key in the POST args. You can either fake it by sending data: {submit:true} or by by removing your if statement and just processing a POST request

    $('.somebutton').click(function() { 
    
            $.ajax({
                url: 'controller/addBookmark',
                type: 'POST',
                data: {'submit':true}, // An object with the key 'submit' and value 'true;
                success: function (result) {
                  alert("Your bookmark has been saved");
                }
            });  
    
    });
    
    0 讨论(0)
提交回复
热议问题