Имеются несколько разных форм в html
с методом POST
или GET
, как заставить jQuery
, отлавливать каким методом был отправлен запрос и заменять type: 'POST',
на нужный.
$(function ($) {
$('button').click(function () {
var frm = $(this).parents('form');
var script = $(this).parents('form').data("script");
$.ajax({
url: './' + script,
data: $(frm).serialize(),
type: 'POST',
dataType: 'text',
success: function (response) {
console.log(response);
$(".response-field").text(response);
},
error: function (error) {
$(".response-field").text(error);
}
});
});
});
<!-- POST метод -->
<form data-script="record_status">
<button type="button">record status</button>
</form>
<!-- GET метод -->
<form data-script="system">
<h2>system info</h2>
<input type="text" id="_model" name="_model" placeholder="_model">
<input type="text" id="_hostname" name="_hostname" placeholder="_hostname">
<input type="text" id="_firmware" name="_firmware" placeholder="_firmware">
<input type="text" id="_version" name="_version" placeholder="_version">
<input type="text" id="_hardware" name="_hardware" placeholder="_hardware">
<input type="text" id="_linux" name="_linux" placeholder="_linux">
<input type="text" id="_systemUptime" name="_systemUptime" placeholder="_systemUptime">
<input type="text" id="_uptime" name="_uptime" placeholder="_uptime">
</form>