Ajax Response throwing JavaScript Syntax Error - javascript

function fnValidatePCRHours(pcr, hoursEntered)
{
$.ajax({
type: "GET",
url : "/TMS/validatePCRHours.html?pcr="+pcr+"&hoursEntered="+hoursEntered,
cache: false,
success: function(data, textStatus, jqXHR)
{
alert(data);
return true;
},
error: function (jqXHR, textStatus, errorThrown)
{
alert(errorThrown);
return false;
}
});
return false;
}
As soon as i get response (IN) from server, i get script error. Can any one help me on this?

Related

error "net::ERR_CERT_AUTHORITY_INVALID" in AJAX

I am trying to make an http request like:
function foo() {
$.ajax({
async: true,
crossDomain: true,
url: "https://192.168.xxx.xxx/api/Domains/GetDomains/false",
type: "GET",
contentType: false,
success: function (data, textStatus, jqXHR) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR.responseText);
}
});
}
$(document).ready(function () {
foo();
});
But Im getting error:
I tried with postman and it worked, why Im getting this error when I try via code?

calling ajax request inside ajax request

I want to call the jquery ajax inside another ajax success response.'request-status' response return correctly and '.table-responsive' div reloaded what I excepted.But in success response when I tried to call 'welcome-mail' I caught Uncaught SyntaxError: Unexpected token ) .I tried as as
$.ajax({
url: 'request-status',
type: 'GET',
data: {'id':user_id,'status':status,'comment':comment},
dataType: 'JSON',
success: function(data, textStatus, jqXHR){
//alert(data);
$(".table-responsive").load(location.href + " .table-responsive");
$.ajax({
type: "GET",
url: "welcome-mail",
data: {'id':user_id},
dataType: 'JSON',
success: function(data, textStatus, jqXHR){
alert(data);
}
});
},
error: function(jqXHR, textStatus, errorThrown){
// alert('errror');
},
});
Route
Route::get('welcome-mail','travelerHome#welcomeMail');
controller
public function welcomeMail(Request $request)
{
$request_data = $request->all();
$id = $request_data['id'];
return response()->json();
}
You are missing a bracket after success, but have an extra }); that doesn't belong there - it should be:
success: function(data, textStatus, jqXHR){
alert(data);
}
});

do something at unsuccessful jquery ajax request

My ajax request is hitting an api with rate limits.
$.ajax({
url:"https://api.themoviedb.org/xxx",
crossDomain: true,
dataType: "jsonp",
dataObj : index,
success: function (response, textStatus, xhr) {
console.log('success');
}
,error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('error');
}
});
I would like to know when I hit the rate limit.
But for the requests showing this in the console :
Failed to load resource: the server responded with a status of 429 (OK)
I don't see 'success' or 'error'.
It's like success and error are not executed.
Is there a way to popup and alert e.g. ?
I tried complete but it does not work either.
Thank you
I could not find documentation for dataObj but removing it seemed to make the query run properly.
If you get rid of it the error callback gets executed and you will be able to see error in the console.
$.ajax({
url:"https://api.themoviedb.org/xxx",
crossDomain: true,
dataType: "jsonp",
success: function (response, textStatus, xhr) {
console.log('success');
}
,error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('error');
}
});
edit:
turns out what you actually want to change is the datatype. Unless you are explicitly also passing a callback you should tell jQuery that you are getting json and not jsonp back.
$.ajax({
url:"https://api.themoviedb.org/xxx",
crossDomain: true,
dataType: "json",
dataObj: index,
success: function (response, textStatus, xhr) {
console.log('success');
}
,error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('error');
}
});
Try using the jQuery statusCode object in the settings, like this:
$.ajax({
url:"https://api.themoviedb.org/xxx",
crossDomain: true,
dataType: "jsonp",
dataObj : index,
success: function (response, textStatus, xhr) {
console.log('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('error');
},
statusCode: {
429: function() {
alert( "Exceeded request limit." );
}
}
});

How to work with Google Suggest queries using jQuery

I am trying to work with the Google API for suggest queries. But I am getting an error.
Here is my code:
$.ajax({
url:'http://suggestqueries.google.com/complete/search?client=chrome&q=sheikh+hasina',
type:"GET",
dataType: 'jsonp',
jsonp:function (data) {
console.log('yes');
},
async:'true',
success:function (data) {
console.log('yes');
},
error: function(jqXHR, textStatus, errorThrown){
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
Assuming that you're trying to get JSONP data from a URL, try this instead.
$.ajax({
url: 'http://suggestqueries.google.com/complete/search?client=chrome&q=sheikh+hasina',
type: 'GET',
dataType: 'jsonp',
success: function (data) {
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});

Why jqXHR.abort() in beforeSend thorws an error?

I am trying to abort ajax call using beforeSend if certain condition is true.
As soon as I call jqXHR.abort() or return false.
I get following error
TypeError: $.ajax(...).fail is not a function
.fail(function (jqXHR, textStatus, errorThrown) {
following is the javascript code
$.ajax({
type: "POST",
beforeSend: function (jqXHR) {
if (1 === 1) {
jqXHR.abort();
}
},
data: { x:1},
url: 'echo/json'
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log("error"+textStatus)
})
.done(function (data) {
console.log("ajax complete");
console.log(data);
});
jsfiddle: http://jsfiddle.net/939xG/
Does anyone know why jqXHR.abort() in beforeSend thorws an error?
or how i can change the code in beforeSend such that I can abort the ajax request without any error.
I am using jquery 1.7.2 and can not upgrade to higher version
When you abort $.ajax in beforeSend, $.ajax will return false rather than a jqXHR. Avoid it by not aborting in beforeSend, or not using done and fail methods in favor of using success and error options instead.
http://jsfiddle.net/939xG/2/
var jqXHR = $.ajax({
type: "POST",
beforeSend: function (jqXHR) {
if (1 === 1) {
jqXHR.abort();
}
},
data: {
x: 1
},
url: 'echo/json',
success: function (data) {
console.log("ajax complete");
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("error" + textStatus)
}
});
console.log(jqXHR) // false
This sounds like a bug to me, but since upgrading jquery isn't an option, i didn't look into that possibility.

Categories