-EDIT-
I have tried to add some error handling to the failing function with error: function(jqXhr, status, error) but jqXhr.responseText is empty so I guess I'm still not handling errors properly.
-/EDIT-
I am new to ajax and have to modify an existing site with these two existing ajax functions, both of which are working fine in Chrome, IE and FF but only the first works in Safari. The second one fails but the error handler (which I guess wasn't set up properly?) doesn't tell me much:
// works in each browser
$.ajax({
cache: false,
type: 'GET',
url: apiBaseUrl + 'GetCountries',
dataType: 'xml',
success: parseCountries,
error: function(){
$('.errorMessage').append('<p>' + errorMessage + '</p>');
}
});
// does not work in Safari
$.ajax({
cache: false,
type: 'GET',
url: apiBaseUrl + 'GetStandardTexts?page=login',
dataType: 'xml',
success: displayRegisteredAlert,
error: function(jqXhr, status, error){
var err = eval("(" + jqXhr.responseText + ")");
alert(err.Message);
}
});
the only real difference between them that I can see is the URL in the second contains and additional parameter, ?page=login.
I finally got this to work. I'm not entirely sure why but adding async:false solved it for Safari. Not required for other browsers.
$.ajax({
async: false, // only required on Safari
cache: false,
type: 'GET',
url: apiBaseUrl + 'GetStandardTexts?page=login',
dataType: 'xml',
success: displayRegisteredAlert,
error: function(jqXhr, status, error){
var err = eval("(" + jqXhr.responseText + ")");
alert(err.Message);
}
});
Related
Edge Browser Issue : Ajax Call to web API is not working in Edge browser, rest other browsers its working fine including IE.
function uploadPDFajaxcall(data, url) {
$.support.cors = true;
$.ajax({
url: url,
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
crossDomain: true,
//contentType: "application/json",
contentType: false,
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('ERRORS: ' + textStatus);
}
});
}
Actual: Edge browser getting Error (catch). rest all browsers working as expected.
Expected: It should hit the web API successfully
I have used ajax calling to get details from database this ajax calling works fine in local but after uploading on server I always get error Resource Cannot be found I have tried many things depend on the solution on the web but no one succeed I even use to create details view and works fine on local but the same error after upload
i have tried
url: '/MobileS/Get/' + MobileSID
url: '~/MobileS/Get/' + MobileSID
url: '../MobileS/Get/' + MobileSID
and also i have tried despite its wrong but i think it may be work
url: '../../MobileS/Get/' + MobileSID
url: '~/~/MobileS/Get/' + MobileSID
url: 'MobileS/Get/' + MobileSID
this is the main function
function getbyID(MobileSID) {
$('#Name').css('border-color', 'lightgrey');
$.ajax({
url: '/MobileS/Get/' + MobileSID,
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
async: true,
processData: false,
cache: false,
success: function (data) {
//var obj = JSON.parse(data)
// alert(data);
$('#Name').val(data.MobileSerial);
$('#myModal').modal('show');
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
The following JSONP JQuery (v1.10.2) call works on all browsers except Safari:-
$.ajax({
cache: false,
type: 'GET',
url: userExistsUrl,
dataType: 'json',
error: function (jqXHR, textStatus, errorThrown) {
var errText = 'We are not able to process your request at the moment (' + textStatus + ', ' + errorThrown + ')';
console.error('HTTP response : ' + jqXHR.status);
console.error(errText);
displayError(errText);
},
success: function (xml) { }
});
On Safari the error function is invoked and the variable textStatus contains the string "error". However if I select the Developer menu option 'Disable cross-origin restrictions' the call works.
As this is just a standard JSONP call, how can I get it to work on Safari without invoking the developer option?
your dataType is wrong
dataType: 'jsonp'
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
question says it all... code:
$(document).ready(function() {
dataToLoad = 'showresults=true';
$.ajax({
type: 'post',
url: 'submit.php',
datatype: 'html',
data: dataToLoad,
async: true,
success: function(data){
$('#results').html(data);
},
});
});
You should use
dataType: 'html'
instead of
datatype: 'html'
and remove the trailing comma at the end (IE<=7 will throw error)
If the problem persists, add an error callback to see if there's an error
error: function(xhr, status, errorThrown){
alert("Error:\n" + status);
}
Hope this helps.
Cheers