jquery post api using param code example
Example 1: axios post data vue js
<template>
<form class="" method="post" @submit.prevent="postNow">
<input type="text" name="" value="" v-model="name">
<button type="submit" name="button">Submit</button>
</form>
</template>
export default {
name: 'formPost',
data() {
return {
name: '',
show: false,
};
},
methods: {
postNow() {
axios.post('http://localhost:3030/api/new/post', {
headers: {
'Content-type': 'application/x-www-form-urlencoded',
},
body: this.name,
});
},
components: {
Headers,
Footers,
},
};
Example 2: vanilla javascript api request
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
const users = JSON.parse(xhr.response);
const content = document.getElementById("user_content");
for(let i in users){
let user_name = document.createElement("p");
let text = document.createTextNode(`User ID ${users[i].id}: ${users[i].title}`);
user_name.appendChild(text);
content.appendChild(user_name);
}
} else {
console.log('The request failed!');
}
console.log('This always runs...');
};
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.send();
Example 3: vuejs get data fromo ajax
<script type="text/javascript">
var ItemsVue = new Vue({
el: '#Itemlist',
data: {
items: []
},
mounted: function () {
var self = this;
$.ajax({
url: '/items',
method: 'GET',
success: function (data) {
self.items = JSON.parse(data);
},
error: function (error) {
console.log(error);
}
});
}
});
</script>
<div id="Itemlist">
<table class="table">
<tr>
<th>Item</th>
<th>Year</th>
</tr>
<tr v-for="item in items">
<td>{{item.DisplayName}}</td>
<td>{{item.Year}}</td>
</tr>
</table>
</div>