POST JSON Data from form without AJAX

前端 未结 1 1522
旧巷少年郎
旧巷少年郎 2020-12-14 03:37

I am trying to POST data to a REST api without using AJAX. I want to send the data in JSON format. I have the following code but am stuck trying to figure out how to conve

相关标签:
1条回答
  • 2020-12-14 04:09

    You can add a hidden input field with the json value, like this -

    function submitform() {
        var url = '/users/' + $('#user_id').val();
        $('#myform').attr('action', url);
        var data = JSON.stringify({
            "userdata": $('#user_data').val()
        })
        $('<input type="hidden" name="json"/>').val(data).appendTo('#myform');
        $("#myform").submit();
    }
    

    You can access your json using json parameter (name of hidden input)

    0 讨论(0)
提交回复
热议问题