Anyone can help me with ajax. I'm making a form and getting data from backend but my ajax is not working.
Here is code
///////////////////////////
<script>
$(document).ready(function() {
$('#trackerForm').submit(function(event) {
$('#items').empty();
var formData = {
'orderId': $('input[name=orderId]').val(),
'email': $('input[name=email]').val(),
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
};
console.log('our data')
console.log(formData);
everything is working fine still here and formData is also working
$.ajax({
type: 'POST',
url: '/shop/tracker',
data: formData,
encode: true
})
But here it;s not working and data is printing something like this {}
console.log(data)
.done(function(data) {
console.log('this is your respponse')
updates = JSON.parse(data);
if (updates.length > 0 & updates != {}) {
for (i = 0; i < updates.length; i++) {
let text = updates[i]['text'];
let time = updates[i]['time'];
mystr = `<li class="list-group-item d-flex justify-content-between align-items-center">
${text}
<span class="badge badge-primary badge-pill">${time}</span>
</li>`
$('#items').append(mystr);
}
} else {
mystr = `<li class="list-group-item d-flex justify-content-between align-items-center">
Sorry, We are not able to fetch this order id and email. Make sure to type correct order Id and email</li>`
$('#items').append(mystr);
}
});
event.preventDefault();
});
});
</script>
Hey, Below is sample **AJAX** request which i used in my project, $("#taskform").submit(function (e) { e.preventDefault(); // avoid to execute the actual submit of the form. var form = $(this); var url = "/api/todo/create/"; $.ajax({ type: "POST", url: url, data: form.serialize(), // serializes the form's elements. success: function (data) { // show response from the php script. loadTodo(); $("#taskform")[0].reset(); }, }); }); Regarding your issue, can you console.log(data) after the success:function call? success: function (data) { // show response from the php script. console.log(data) error: function(data){ console.log(data) }