Call error only on some browser - javascript

I've made this ajax call:
$.ajax({
//query rest che trova tutti gli amici dell'utente corrente
type: 'GET',
async: false,
url: "http://www.timeapi.org/utc/now",
success: function(data) {
time_now=data;
},
error: function(data) {
console.log("ko" );
}
});
in chrome works perfectly but in firefox and on mobile android goes in error callback.
In firefox the error is(written in red):
GET 'http://www.timeapi.org/utc/now.json 200 OK

This server must be old, it doesn't set any cross-origin authorization header.
But luckily, the home page explains it's JSONP compatible and gives an example :
<script type="text/javascript">
function myCallback(json) {
alert(new Date(json.dateString));
}
</script>
<script type="text/javascript" src="http://timeapi.org/utc/now.json?callback=myCallback"></script>
You can also adapt your code :
$.ajax({
type: 'GET',
dataType: "jsonp",
url: "http://www.timeapi.org/utc/now.json?callback=?",
success: function(data) {
time_now=data.dateString;
},
error: function(data) {
console.log("ko" );
}
});
Note also that I removed the async:false. Not only is it incompatible with JSONP, it's also always a bad idea.

Related

JQuery ajax get json data

how to get image url from "https://api.qwant.com/api/search/images?count=10&offset=1&q=cars" api using jquery. i'm unable to do it. bellow i've attached my code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
url:"https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
type:"GET",
crossDomain : true,
async: false,
dataType: "jsonp",
jsonpCallback: "myJsonMethod",
success: function(json) {
$.parseJson(json)
},
error: function(e) {
console.log(e);
}
});
function myJsonMethod(response)
{
console.log(response);
}
</script>
Your request isn't working because of CORS, which is enabled on the API server. You need a proxy server to workaround this. For development purposes you could use a free online proxy server, your code then simplifies:
$.ajax({
url:"<PROXY:SERVER>https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
success: function(json) {
// Do stuff with data
},
error: function(e) {
console.log(e);
}
});
As an example check out this working fiddle.
Try this
$.ajax({
url:"https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
type:"GET",
crossDomain : true,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
jsonpCallback: "myJsonMethod",
success: function(json) {
$.parseJson(json)
},
error: function(e) {
console.log(e);
}
});
This is a CORS thing, so you can server all this up from a web server, like http-server, and I think certain browsers like Firefox allow this to occur locally.
There are some flags with Chrome that'd allow it to work locally like this too, I believe.
Cheers

calling http url is working but Https is not working

I created self-signed certificate and bind with site for using https protocol and also its working fine in IIS but problem is that whenever I access contained webservice url(https://webservice/webmethod) into jquery ajax call for posting data and its cant work.
enter code here
<script src="jquery-1.9.1.js"></script>
<script type="text/javascript">
function btncallWebService() {
$.ajax({
//url is just for understanding
url: 'https://localhost/.../EditFormServices.asmx/WebSvcSave',
data: { sData:"bbbb" },
method: 'post',
dataType: 'xml',
success: function (respo) {
alert("success"+respo.d);
},
error: function (error) {
alert("error");
}
});
}
</script>
Here we go:
Your url is wrong
<script type="text/javascript">
function btncallWebService() {
$.ajax({
//url is just for understanding
url: '/WebSvcSave',
data: { sData:"bbbb" },
type: 'POST',
dataType: 'xml',
success: function (respo) {
alert("success"+respo.d);
},
error: function (error) {
alert("error");
}
});
}
</script>
Hope it helps;)

Ajax request not working when a function is added to the success option

I am having trouble getting an ajax GET request (or any request for that matter) to retrieve the response. I am simply trying to return the response in an alert event:
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'Get',
url: 'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=XXX&scope=crmapi&criteria=(((Potential Email:test#email.com))&selectColumns=Potentials(Potential Name)&fromIndex=1&toIndex=1',
dataType: 'json',
success: function(data) {
alert(data);
}
});
});
});
</script>
I can get this and other similar post requests to work by taking away the function in the success option and editing the code like this:
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'Get',
url: 'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=XXXX&scope=crmapi&criteria=(((Potential Email:test#email.com))&selectColumns=Potentials(Potential Name)&fromIndex=1&toIndex=1',
dataType: 'json',
success: alert('success')
});
});
});
</script>
Why is this? And more importantly, how can I retrieve the response data and transfer it to an alert message? Any help is appreciated!
** Update:
Upon reading the first two users' responses on this question, this is what I have:
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'GET',
url: 'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=418431ea64141079860d96c85ee41916&scope=crmapi&criteria=(((Potential%20Email:test#email.com))&selectColumns=Potentials(Potential%20Name)&fromIndex=1&toIndex=1',
dataType: 'json',
success: function(data) {
alert(JSON.stringify(data));
},
error: function(data) {
alert(JSON.stringify(data));
}
});
});
});
</script>
I am able to get the error response, so I can confirm there is some kind of error. I also want to point out that I am making the request from a different domain (not crm.zoho.com) so should I be using jsonp? If so, how would I alter the code?
When you have
success: alert('success')
you do NOT have a successful request, you are actually executing this function at the start of AJAX method. The success parameter requires a pointer to a function, and when you use alert('success') you are executing a function instead of providing a pointer to it.
First thing that you need to try is to update type to GET instead of Get:
$.ajax ({
type: 'GET',
Try using the .done() function as follows:
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'Get',
url: 'yourUrl',
dataType: 'json',
}
}).done(function(result) {alert(data);}) //try adding:
.error(function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);})
});
});
the error function will also give you some information in your console as to the status of your request.

IE 9, 10, 11 - Ajax calls not returning

I am having an issue with IE related to jQuery and ajax. Chrome and Firefox work just fine, but my ajax calls are disappearing in IE.
After making the ajax call, neither the success nor the fail functions are being called. I can see the response in the IE console, and I know my controller action is being hit.
$.ajax({
url: controllerUrl
type: 'POST',
dataType: 'json',
cache: false,
data: {
id: customerId
},
success: function () {
alert('success!');
},
error: function () {
alert('failed!');
}
});
Has anyone else seen this issue?
fail: function () {
alert('failed!');
}
fail is not a valid jQuery ajax setting. I believe you are looking for error.
Also, cache: false, does nothing with POST requests.
Note, jQuery does not append the time stamp with POST requests.
The source code clearly demonstrates this. (summarized from https://github.com/jquery/jquery/blob/master/src/ajax.js)
var rnoContent = /^(?:GET|HEAD)$/;
s.hasContent = !rnoContent.test( s.type );
if ( !s.hasContent ) {
/* code to append time stamp */
}
You are missing a comma , after your URL parameter:
$.ajax({
url: controllerUrl, // <--- you were missing this comma!
type: 'POST',
dataType: 'json',
cache: false,
data: {
id: customerId
},
success: function () {
alert('success!');
},
error: function () {
alert('failed!');
}
});

Uncaught syntax error: Unexpected token < , ajax call

<script>
(function(){
var searchURL = 'http://en.wiktionary.org/wiki/search';
$.ajax({
type: "GET",
url: searchURL,
dataType: "jsonp",
cache: false,
async:false,
success: function(responseData, textStatus, XMLHttpRequest){
iframe(responseData);
}
});
})();
</script>
I added this script to my html file and it is show the following errors, copy pasting the function in console is also showing the same errors.
Uncaught SyntaxError: Unexpected token <
Resource interpreted as Script but transferred with MIME type text/html
could anyone help me resolve this issue, I am using Chrome brower.
You can't request arbitrary pages via AJAX, and jsonp doesn't magically make that work. You need to use the Wiktionary API.
The URL is http://en.wiktionary.org/w/api.php.
$.ajax({
url: 'http://en.wiktionary.org/w/api.php',
dataType: 'jsonp', // will automatically add "?callback=jqueryXXX"
cache: true, // the API complains about the extra parameter
data: { // the parameters to add to the request
format: 'json',
action: 'query',
titles: 'test'
},
success: function(data){
console.log(data);
}
});

Categories