I'm trying to make a post request to json file using JQuery, and for the server I'm using npm
package live-server
. But I got this error in chrome console:
POST http://127.0.0.1:8080/user.json 405 (Method Not Allowed)
here's my index.js:
$('#new').submit(function (e) {
e.preventDefault()
const fname = $('.fname').val();
const lname = $('.lname').val();
const url = $(this).attr('action');
$.ajax({
method: 'post',
url: 'user.json',
data: { fname, lname },
dataType: 'json'
}).done(function (data) {
console.log(data);
});
})
my index.html:
<form id="new" action="user.json">
<label for="">fname</label>
<input class="fname" type="text" placeholder="fName">
<label for="">lname</label>
<input class="lname" type="text" placeholder="lName">
<input type="submit" value="submit">
</form>
the json file I wanna make the request to, user.json:
[
{
"fname": "fola",
"lname": "abdallah"
},
{
"fname": "maha",
"lname": "abdallah"
},
{
"fname": "Moe",
"lname": "abdallah"
}
]
when I make get request it works very well, so what's the issue exactly I could be missing the obvious