How to capture the native http network errors in jquery - javascript

In JQuery AJAX while calling a REST API, As per the functionality the negative use cases needs to be logged.
In the chrome console this can be seen
net::ERR_ADDRESS_UNREACHABLE
net::ERR_NETWORK_CHANGED
net::ERR_NAME_NOT_RESOLVED
how to capture these exact errors in the error callback or any other means
The JQuery AJAX Code is
$.ajax({
type: 'GET',
url: <my url>,
contentType:'application/json',
dataType:'text',
beforeSend: function (xhr) { },
data: "",
success: function (responseData, textStatus, jqXHR) {
//success code
},
error: function (responseData, textStatus, errorThrown) {
console.log(arguments);
//here I need to capture the exact network error
}

Related

Cordova Ajax call throwing net::ERR_SPDY_PROTOCOL_ERROR

I have two Ajax calls but one of the Ajax calls are returning net::ERR_SPDY_PROTOCOL_ERROR.
The Ajax call urls are coming from WordPress Woocommerce RestApi.
When I run chrome in mode - chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security - the error disappears and everything is working correctly.
Error
Ajax call that is not working:
$.ajax({
url: "https://krii.000webhostapp.com/wp-json/wc/v2/products/categories?per_page=99",
success: function(json){
console.log("Success", json);
$.each(json, function (index, categories) {
//console.log(categories);
catego.push({Cat_Name: categories.name});
//console.log(catego);
$('select#categories2').append('<option data-id="> categories.id">'+categories.name+'</option>');
});
},
error: function (XMLHttpRequest, textStatus, errorThrown){
console.log(textStatus,errorThrown)
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('ck_...:cs_...'));
},
type: 'GET',
contentType: 'json'
});
Ajax call that is working:
$.ajax({
url: "https://..../wp-json/wc/v2/products/categories?per_page=99",
success: function(json){
console.log("Success", json);
$.each(json, function (index, categories) {
//console.log(categories);
catego.push({Cat_Name: categories.name});
//console.log(catego);
$('select#categories').append('<option data-id="> categories.id">'+categories.name+'</option>');
});
},
error: function (XMLHttpRequest, textStatus, errorThrown){
console.log(textStatus,errorThrown)
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('ck_...:cs_...'));
},
type: 'GET',
contentType: 'json'
});
How exactly I can fix this, cause it does not work on mobile device.
This error ERR_SPDY_PROTOCOL_ERROR is found in Google chrome. With latest updates of google chrome it is depracted. This protocol was added for faster web load and security. You can learn more from how-to-fix-err_spdy_protocol_error-in-google-chrome-2019

how to get the returned URL from ajax request

i'm trying to get only the returned URL from ajax request
like this
$.ajax({
type: "GET",
dataType : "jsonp",
async: false,
url: $('#FaceBookProfileLink').attr('href'),
success: function(response) {
console.log(response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
but i can't.
the returned URL showing in the console as js file but i can't get it
please any help and many thanks in advance.
As you are using 'GET' method, retrieve them with $_GET[], ie:
echo $_GET['code']

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." );
}
}
});

error in accessing DB through ajax request

When i run url of following code in Firefox poster it runs successfully, but when i try to run with help of $.ajax in my js file of mobile app it gives me "ERROR". why?
(order_id is not the issue)...
$('#page_custinfo').bind('submit',function(){
$.ajax({
url: "http://localhost/putDetails.php?order_id=3&fname=seth&lname=rahul&email=ol#gmail.com&s_add1=karveroad&products=nokia&order_status=pending&order_total=1000&city=pune&postal_code=033&country=india",
type: 'GET',
contentType: 'application/x-www-form-urlencoded' ,
success : function (serverResponse) { alert("success"); },
error : function (jqXHR, textStatus, errorThrown) {alert("ERROR"); }
});
});

Receive serialized in php data by using ajax

I have a php script, which return serialized in php data. And I try to receive this data by using $.ajax() method from jQuery 1.7. Here is the example.
$.ajax({
url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
type: 'GET',
dataType: 'text',
cache: 'false',
complete: function(res) {
alert('COMPLETE() done');
console.log(res);
}
});
In console I see only
Object { readyState=0, status=0, statusText="error"}
So, what I do wrong? Could you help me please?
UPD
Interesting notice: if I use JSONP dataType request can receive data, but can't process it.
Here is an example.
$.ajax({
url: 'http://input.name/get.php?do=lookup',
data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
dataType: 'jsonp',
cache: false,
success: function(data) {
alert("Data: "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error: "+textStatus);
console.log(jqXHR);
}
});
Instead of complete: use success: then res will be the returned data from your ajax request.
Remember to use error: as well incase there is an error with you call, as it seems that there might be in your console output.
Code:
$.ajax({
url: 'http://input.name/get.php?do=lookup',
data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
cache: false,
success: function(data) {
alert("Data: "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error: "+textStatus);
console.log(jqXHR);
}
});
Your code is probably fine, but you're trying to violate the same origin policy. Basically, if your site is http://aaa.com/, you cannot make AJAX called to http://bbb.com/.
There are a few ways around it:
Getting around same origin policy in javascript without server side scripts
But most of them require that both sides play nice.
The response is the second parameter of complete function:
$.ajax({
url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
type: 'GET',
dataType: 'text',
cache: 'false',
complete: function(res,response) {
alert('COMPLETE() done');
console.log(response);
}
});
More info: http://api.jquery.com/jQuery.ajax/
You should also consider using JSON, not php serialized data

Categories