Get response from pending webservice with ajax - ajax

I have a web service which is executed with pending status in the browser , how can i get it's response because it did not go into succes function nor the error one.
Here is my code :
$.ajax({
type: "get",
url: "https://domain_name/listenrealtime",
data: {username: '', password:"" },
headers: {},
success: function (data) {
console.log("success test");
console.log(data);
},
error: function(data){
console.log("error test");
}
})
here is a capture from the browser:
enter image description here

Related

React AJAX post request does not go to success/error method

I have a React Component which calls an AJAX POST request.
The POST request creates an entry in the database as expected. (status 201 returned) However the success/error methods of the same are not called.
$.ajax({
url: '/api/put/comment/',
dataType: 'json',
type: 'POST',
data: comment,
beforeSend: function(xhr) {
console.log('before send');
xhr.setRequestHeader('X-CSRFToken', getCookie('csrftoken'));
},
success: function(data) {
console.log('success');
this.loadData();
},
error: function(xhr, status, err) {
console.log('error');
this.setState({data:comments});
console.error(this.props.url, status, err.toString());
}
});
The request is called through a button click. Also the browser hangs once the request is made, and the solution is to open a new tab.
Also i do not see the POST request on the Network tab of Chrome, but see it on the backend.
Any hints?
Try using ES6 arrow functions or bind your functions with this
$.ajax({
url: '/api/put/comment/',
dataType: 'json',
type: 'POST',
data: comment,
beforeSend:(xhr) => {
console.log('before send');
xhr.setRequestHeader('X-CSRFToken', getCookie('csrftoken'));
},
success:(data) => {
console.log('success');
this.loadData();
},
error:(xhr, status, err) => {
console.log('error');
this.setState({data:comments});
console.error(this.props.url, status, err.toString());
}
});

NODE.js - How to make an AJAX request from the DOMContentLoaded listener

I am trying to send data via an Ajax request from the DOMContentLoaded listener in my global.js
The following code produces error 400 (Bad request), and I'm not clear why. If I submit the same payload using Postman to that Url, I get success.
Global.js
document.addEventListener('DOMContentLoaded', function() {
var obj1 = document.getElementById('myObject');
// A bunch of logic to retrieve data to be sent to the server
// that if successful triggers a callback function
ProcessEvent: function (info){
// Update the database record here
//
alert("Making an AJAX call");
var xcall = $.ajax({
url: "http://127.0.0.1:3000/cashiers/jsonset",
type: 'post',
contentType: 'application/json',
data: obj1.items,
dataType: "json",
success: function(json) {
alert('Success : '+ json.reponse);
},
error: function(res){
alert("Error "+res.status+" with "+res.statusText);
}
}).done(function( msg ){
alert( "Data Saved: " + msg );
});
}
});

the alert inside my success function response on ajax wont appear?

$.ajax({
type: "POST",
url: url,
data: data,
success: function(response){
alert("success"); --> this is the one
}
});
I've successfully inserted the data from my sign-up form i just want to inform the user that they successfully registered on the alert box
Maybe it's not succeeding. Have you tried throwing in an error handler to see if it's failing?
$.ajax({
type: "POST",
url: url,
data: data,
success: function(response){
alert("success"); --> this is the one
},
error: function (XHR, status, error){
console.log('error', error);
}
});

Ajax POST request to ODATA service throws Invalid HTTP status code 501

I have the following code which I wish to use for inserting a record into an entity of an ODATA service.
The post request throws a XMLHttpRequest cannot load Invalid HTTP status code 501.
Apparently GET request works fine. Can anyone suggest a way to find out the problem ? Does my WCF Service has a problem ?
var data = { application_id: 777 , user_id: 'test' };
var data = JSON.stringify(data);
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
datatype: 'json',
url: oDataURL + 'application_admins',
data: data,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
account = data.d;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});

How to use an ajax call's response to manipulate a dynamic page?

I am trying to submit a form with the user's inserted data and get the html back from the page called (update.asp).
How do I get the html response and how do I write it to a div on the page? The response would be "success".
If my page throws a 500 or other type of error, how can I handle that?
$('input#btnUpdate').click( function() {
$.ajax({
url: 'update.asp',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
// how do i catch the response? is this the right place?
},
error: function(data) {
// how do I catch the error code here?
}
});
The response from the server in both cases would be passed to the callback as the data variable in your example. Try using console.log(data) inside of your callbacks to see the result in your developer console.
$('input#btnUpdate').click( function() {
$.ajax({
url: 'update.asp',
type: 'post',
dataType: 'json',
data: $('#myForm').serialize(),
success: function(response) {
$("#yourDIV").html(response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError); //output, 500
}
});
});
More on this: ajax()

Resources