Send a success response to POST request - javascript

I've got the following ajax call:
function sendRequest(quote, author){
$.ajax({
type: "POST",
url: window.location.pathname,
data: {quote: quote, author: author},
dataType: JSON,
success: function(){console.log("Data sent!");},
error: function(error){console.log("ran into an error")}
});
}
And here is my server (using express) handling the post request,
app.post("/", function(req, res){res.status(200).end("Success!")});
However, the console doesn't print "Data Sent". Instead, it prints "ran into an error"
Everything else is tested and is working correctly

First of all dataType should be 'json' not JSON
Then check the console log for specific error.

Related

AJAX Post failing despite data existing

I am trying to send some data to my server using an AJAX Post call. However, whenever I run the function containing the ajax call I get a server error. Here is my AJAX call (I am trying to send a string and put it inside json for the purposes of this call):
function sendFileName(){
data_to_send={"name": scriptName};
data_to_send=JSON.stringify(data_to_send);
$.ajax({
url: '/filename',
type: 'POST',
dataType: 'json',
data: data_to_send,
error: function(resp){
console.log("Oh no...");
console.log(scriptName);
console.log(resp);
},
success: function(resp){
console.log('Sent file name!');
console.log(resp);
}
});
}
When I log the scriptName I get it in the console, so the data exists. I'm assuming the issue has to do with the way in which I'm sending it?
Here is the server-side code as well, where when I log the req it shows up as undefined:
app.post("/filename", function(req,res) {
file = req.body.name;
console.log(file);
});
Would really appreciate any help I can get with this!

Ajax response is fine but in error method

I'm facing a problem in ajax call. When I have called to given URL, Everything is fine but it is coming in error method of ajax response,
Code is below
$.ajax({
url: "My_URL",
Method: "Get",
async: false,
success: function(msg){
console.log("success");
console.log(msg);
},
error: function(msg){
console.log("error");
console.log(msg);
}
});
Please see the attached image which is fine but response in error method. Why ?
Any can guide me please ?

Jquery: Probably a syntax Issue in ajax() method - Value not getting sent

I'm able to dump value of the variable message in console .
But im not able to send it off in POST Request.
AJAX call:
chat.throwmsg = function(message) {
if ($.trim(message).length != 0) {
console.log(message);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: { method: 'throw', message: message} ,
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
}
}
This maybe due to wrong syntax, but I've tried both single and double quotes, and combination as well .
With a wild assumption, you are not having any error messages in developer console and chat.php has a post handler forthese parameters
Since your data is JSON, please change the code as this way and have a try..
var temp={ method: 'throw', message: message};
var param=JSON.stringify(temp);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: param ,
dataType: "json",
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
after reviewing the code I could not find any issues that restrict the data to be sent along with ajax request,if you are having any syntax errors you should have been warned prior the request initialization, however I recommend you to check the request header in the network tab of browser console and see your sending data along with the request if it's there you probably need to check the code of getting the post data in your server-side implementations

"Uncaught SyntaxError: Unexpected token <" after successful HTTP Request

I am trying to do AJAX using jQuery with an ODATA REST service provided by an SAP backend system.
$.ajax({
type: 'GET',
url: 'http://sapxx.sapms/sap/opu/odata/.../',
async: true,
dataType: 'jsonp',
username: 'username',
password: 'password',
crossDomain: true,
success: function() {
alert('Done.');
},
error: function() {
alert('Error.');
},
});
The request returns status 200 and I get a response from the server which is called something like "?callback=jQuery3100687..." and contains xml code. All this is visible in the Chrome debugger. But after the successful HTTP Request I get the aforementioned error
Uncaught SyntaxError: Unexpected token <
I suspect the error is due to the "dataType" parameter which is set to "jsonp" in the request. Is there any way to work around this error? The server can only respond using XML format. The request only works when the dataType is set to "jsonp", I guess because it enables CORS. After sending the request, I get the "Error" alert despite the 200 status.
You say you get an xml response from the server. Your ajax request is set up for jsonp, though:
dataType: 'jsonp',
Try this instead:
dataType: 'xml',
success: function(xml) {
//remainder of the code
}
If you set the dataType to jsonp, jQuery will try to parse the response as JSON. This results in errors, because the response ins't JSON.

Unexpected token from webservice

I got this little snippet where I call a Battlefield 3 stat server. If you visit this URL, which I'm calling, we'll be getting no errors:
http://api.bf3stats.com/pc/server/?output=json&id=534f7035-cef8-48aa-b233-8d44a0956e68
But when I try to get the stats via Ajax call, I get:
Uncaught SyntaxError: Unexpected token :
... In my console I can see that the response is coming in, as when I visit the url, but I can't get the data with the ajax call... Is there something wrong with my code???
$.ajax({
type: "GET",
url: "http://api.bf3stats.com/pc/server/?output=json&id=534f7035-cef8-48aa-b233-8d44a0956e68",
dataType: "jsonp",
success: function(response) {
console.log(response);
}
});
Thank you in advance...
I'm not entirely sure, but it seems like that server is reporting 500 Internal Server Error when getting it via ajax. I've tried a number of different methods, and they all have 500 Internal Server Error returned.
That site seem to be unresponsive at certain times. Interesting also that the JSONP datatype did not work in Firefox. I've added a simple check to the response here.
$.ajax({
type: "GET",
url: "http://api.bf3stats.com/pc/server/?output=json&id=534f7035-cef8-48aa-b233-8d44a0956e68",
dataType: "json",
success: function(response) {
if (response == null) {
alert ("An error has occurred!");
} else {
console.log(response);
}
}
});
}

Categories

Resources