JSON-P request to URL does not work? - javascript

I've been trying to get this to work for about three hours now. Searched around, looked all over actually, and I tried all the examples people showed, none of which worked. This is really starting to bug me. What I'm trying to accomplish is a call to BitCoin Charts JSON file that holds all the bitcoin data. I am setting up a webstore, and would like the price to be accurate when the user loads the page.
Here is the snippet of code where I call the $.getJSON() function:
function JSONCall() {
var url = "http://api.bitcoincharts.com/v1/weighted_prices.json";
$.getJSON(url + "?callback=?", Update);
}
function Update(data) {
//there will be code here to change the HTML on my site, but for now, this works to test
console.log(data);
}
The current error that I'm experiencing is:
Resource interpreted as Script but transferred with MIME type text/html: "http://api.bitcoincharts.com/v1/weighted_prices.json?callback=jQuery19100276493770070374_1387411109377&_=1387411109490". jquery-1.9.1.js:8336
Uncaught SyntaxError: Unexpected token :
and I cannot for the life of me get it to work. My code looks fine, according to everything I've seen this far. If anybody knows more about this than me and would be willing to help, that would be fantastic! Thanks in advance.

I checked the URL and it doesn’t return JSONP, only plain JSON.
You will need to find another way, some options come to mind:
Do a CORS request (cross-origin) if the service supports it
Run via a server-side proxy
See if the service supports JSONP in some other way

Related

Using Ipinfo.io in Javascript

Okay, so, I'm trying to pull the correct region and city from Ipo.io but it isn't working. When ran locally it's giving me one location, which is wrong, and when deployed it also gives me a location but it's also wrong.
What I'm Trying To Do: pull the correct locations when user clicks a button.
The html is fine, so no need to add it. The js file where the request is looks like so (didn't include the whole file, just the relevant parts).
showMe is a modal that pops up once a user is scrolling the screen. Everything works fine the location is just wrong. I looked at the docs and it doesn't say anything about Javascript, so I'm writing to you guys in hopes that this tool actually works with Javascript.
Any help would be appreciated, thanks!
$('#showMe').click(() => {
$('#time-text').empty();
$.get("https://ipinfo.io?token=insert-token", function (response) {
$('#time-text').append(response.region);
}, "jsonp")
});
also, just to be clear, i've also tried this - as given in their documentation, but it throws an error.
$.get("https://ipinfo.io", function (response) {
$('#time-text').append(response.region);
}, "jsonp")
The error I am receiving for the last implemention is:
jquery-3.4.1.min.js:2 GET https://ipinfo.io/?callback=jQuery34105473588412770183_1578083636662&_=1578083636663 net::ERR_ABORTED 429
For the first implementation, there is no error - just wrong location.
You're getting a 429 Error, which means you've hit the request limit for the Day / Month (Depends)
Here is the relevant part of the the Docs discussing the Rate Limits

getJSON works for some addresses, but not others

Having a strange problem. I'm using getJSON to retrieve information online using the following code:
$.getJSON("url", function(json) {
$("#quote").html(JSON.stringify(json));
});
Strangely, I have only been able to get the above code to work when I use https://api.whatdoestrumpthink.com/api/v1/quotes/random as the source. All other sources I have tried, such as http://quotes.rest/qod.json do not seem to return anything. I am currently using codepen to make my page.
I can retrieve quotes from the first URL perfectly and access everything how I'd like to, but not with anything else. I have tried using Get and ajax methods as well but to no avail. I'm relatively new to code so there must be something obvious I'm missing?
Thanks for any help.
Looks like the server uses https protocol. Correct the URL and try again. Or provide logs if in case the request fails.

jQuery.tokenInput.js script in JavaScript not working

I am making an application that is purely out of JavaScript (frontend and backend). So now I am using jQuery.tokenInput.js and I am having some troubles with the plugin recognizing the script.
First of all, it's not logging any error messages so I don't even know if it's an issue on my end or not.
I've essentially created a route in the application /autocomplete/tags and it accepts q parameter as well.
So when I type in something like this /autocomplete/tags?q=r I get the following result on the page
[{"tag_name":"Android","_id":"ooJaBpZ6MShmzbshY"},{"tag_name":"RPG","_id":"KpvAqCRqKKP5rbGLD"}]
So now when I initialize the plugin like this
$('#tag_input').tokenInput("/autocomplete/tags", {
theme: "facebook",
propertyToSearch: "tag_name",
tokenLimit: 5
});
It changes the input and everything. I've even tried with constant data and it seems to work but not with a script for some reason.
Is there a way I can debug/troubleshoot? Can I somehow turn on logging for this plugin? I don't actually see any issue with the way that I am doing it. I've looked at the demos and they return JSON in exactly the same way.
If you've got any ideas, it would be great!
The JSON returned from an external service must be returned under an application/json header type - we found that this service was returning text/html instead.
Information about how to specify the content type with Meteor can be found on this question.

d3.json Troubles

I've spent a good portion of my evening working on this.
I am trying to get this live json data http://citibikenyc.com/stations/json loaded onto a page on my website using d3.json.
Just testing:
var bike = "http://citibikenyc.com/stations/json";
d3.json(bike, function(error, json) {
console.log(json);
});
doesn't work. I get the error
XMLHttpRequest cannot load http://citibikenyc.com/stations/json. Origin http://hochemoche.com is not allowed by Access-Control-Allow-Origin.
Is there something that I'm missing? Is there possibly some restriction coming from the page which I'm submitting my request to? Any help on this would be greatly appreciated. I don't believe that there is any restriction coming from citibike's site because this same request works as a curl from my Terminal, also other's have used this data, but can't seem to figure out how they are loading it based on their code.
eg: http://jehiah.cz/citibikenyc
#Blaklaybul
The Access-Control-Allow-Origin parameter must be set by the JSON source, in this case it is "http://citibikenyc.com/stations/json".
Try to include the CORS parameters in the header of the ajax request you are making. See http://techblog.constantcontact.com/software-development/using-cors-for-cross-domain-ajax-requests/
You can try other alternatives like jsonp etc, again information of which is available in the above link.
If you use Chrome browser, you might also use an extension namely "Allow-Control-Allow-Origin: *". After installing and launching it, I believe it will work.
Hope all thing will be ok!

Converting jquery code to prototype for a cross browser Ajax request in order to obtain Latest Tweets

Converting jquery code to prototype for a cross browser Ajax request
My first post !
I had to fetch my latest tweet and so had to perform a cross browser request. The current app uses prototype, but I am somewhat familiar with jquery.
So, I began in jquery as :
$.ajax('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?', {
dataType: "jsonp",
success:function(data,text,xhqr){
$.each(data, function(i, item) {
console.log(item.text);
});
}
});
I get a warning as :
'Resource interpreted as Script but transferred with MIME type application/json.'
But, I do get to see my last tweet. Fine.
So, I decided to do the same in prototype and then try eliminating warnings, if any. But, I got nowhere near even after trying for hours.
This is what I came up with initially in prototype. I had a lot of changes/alterations that followed but none worked.
new Ajax.Request('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?', {
contentType: "jsonp",
onSuccess:function(transport){
console.log(transport) ;
}
});
The request succeeds but the response text is nil / " " . I get no error in Firefox but in Chrome the error is :
XMLHttpRequest cannot load http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?. Origin http://localhost:4000 is not allowed by Access-Control-Allow-Origin.
Refused to get unsafe header "X-JSON"
Any help shall be greatly appreciated. Thank you.
Thanks Darin for getting me back onto Dandean's JSONP for prototype js.
Although I did not mention in the first place(the question was getting a bit long), I had tried using Ajax.JSONRequest from Dandean (the very link you are referring to). The request was constantly getting failed, and I didnt go further into using it as I was assuming it would be pretty straight forward getting it done in prototype too, like jquery. As I got no more answers, justifiably so, I decided to wrap my head around using Ajax.JSONRequest. The request failure was not due to a gateway timeout. It was because the request url had params callback repeated in it.
So, the request url turned out to be
GET (twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&&callback=?&callba‌ck=_prototypeJSONPCallback_0)
Thus, I defined my url without callback and it performed as desired.
However, I still get a warning :
Resource interpreted as Script but transferred with MIME type application/json
Here is the equivalent prototype :
new Ajax.JSONRequest('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1', {
onSuccess:function(response){
response.responseJSON.each(function(item){
console.log(item.text);
});
You may take a look at the following page.
Regarding the error [Refused to get unsafe header "X-JSON"], this can happen if your page is under SSL, but the URL referenced in your AJAX call is not also an HTTPS URL.

Categories

Resources