download json file instead of console.log-ging it in $.getJson() - javascript

Is there a way I can download a json file instead of getting with jQuery's $.getJSON() and logging the callback function's argument, which is the json?
I don't want to manually type it from the console.log because it is long. Is there a way I can print it perhaps, and not formatted, so I can copy - paste it into a .json file?
I can't just go to the URL as I'm passing some json to the url as $.getJSON()'s second argument to access the response json.

try using
JSON.stringify(response);
There are also many websites and plugins that will let you make POST requests from your browser such as hurl.it or Postman (Chrome app).

Related

text/html output when requesting JSONP

I have been playing around with the jQuery library the last week or two.
Very handy! I am now playing with the AJAX requests to retrieve things such as the weather, current downloads and more, which have been going well so far!
I have now tried to connect up to my ISP to get my current data usage (peak, off peak etc).
When I use Chrome, I can manually type the variables into the URL and have the required JSON code show in the browser. The issue is, that it seems to return text/html instead of application/json.
When you go into developer tools, it shows text/html. This make it difficult for me to retrieve the data from my home server using AJAX and JSONP. See here for a failed query (but you can still see the text/html output, which is in a JSON format! Failed JSON Query on ISP
My question is, how could I get this data from the server URL, then make it into JSON that jQuery can read?
When I try the .load , $.get functions I run into Cross Origin Issues...
EDIT:Here is the PDF documentation for the API (Download at the bottom of the page)
Notice that I need to append certain values (user / pass / token). My ultimate aim is to have my JS read these values and store them.
The issue is, that it seems to return text/html instead of application/json.
That's a serverside issue. Go and file a bug report.
This make it difficult for me to retrieve the data
Not by itself. You should be able to override the settings how responses are parsed, e.g. in jQuery by using the datatype parameter.
using AJAX and JSONP
Notice that you cannot use JSONP, as it is not supported by that API (judging from the docs and a simple ?callback=test try). If you want support for that, file a bug report against the service provider.
When I try the .load, $.get functions I run into Cross Origin Issues...
Yes. They don't send CORS headers either. I suspect that this API is only used internally, and by devices that are not subject to a same-origin policy.
how could I get this data from the server URL, then make it into JSON that jQuery can read?
Use a proxy on your own server (that runs in the same domain as your app). It can also fix that content-type header.
For more details see also Ways to circumvent the same-origin policy, though most of the methods require cooperation of the service provider (to implement serverside features).
If i understand you correctly You ask for a certain value and it gives you a string. For most API's in the world they send a string that you have to parse into JSON or some language code. I would suggest looking at Parsing JSON Strings link. It explains how to take well formated strings and parse them into JSON readable objects.
var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );
if you go on further and start using php take a look that Parsing JSON Strings with PHP
EDIT:
use .done() method to grab text from other pages after AJAX call.
$.ajax(...).done(function(html){
//do what you want with the html from the other page
var object = $.parseJSON(html)
}

In javascript, is there a way to turn a url into a data-url?

I would like to take a regular old url and turn it into a data url that encodes the entire file. Is there a way to do that?
Alternately, the question could be: is there a way to get a File object for a given url? (because then I could use a FileReader to call readAsDataURL).
take a regular old url and turn it into a data url that encodes the entire file
…means that you need to get the data from the file. This can be done by using an XMLHttpRequest (as long as the URL points to your own domain). Then take the loaded data, pass it to btoa, and concatenate it with the response's Content-Type header.

Call JS through cURL

I am trying to call my js file through cURL. That JS file in-turn call's third party API to fetch me the result.
Overview of JS file.
In js file i am creating an object of the 3rd party library file and sending them the relevant data to get the response back. This response need to be captured and returned back when call'd through cURL.
Procedure of calling cURL
The cURL function will be triggered as an API call.
What i have tried so far !
I am storing the response of the library file in a cookie. So that i can use the same. I am able to achieve this through browser. But, when tried with REST client, i get all my <script> function as response also it doesn't call the library file too.
So, how can i call the JS file through cURL OR is there any other alternative way to achieve this. ?
NOTE : It is not a regular browser flow, it has to be a API call.
Please let me know on this !

How to get data from remote website?

Lets say there is a url out there e.g. www.website.com/data.jsp
the link has the following JSON data
{"successful":"true","rows":[{"zip":"65472","user_id":"10843","name":"Rufio"}]}
I just want to be able to extract this data at runtime however I am having a hard time getting it using getJSON
$.getJSON("test2.jsp",function(result){
$("div").append(result.rows[0].user_id + " ");
});
Now if I run it using a local file with the data residing in test2.jsp as shown above it appends the user_id. However when I try to access "www.website.com/data.jsp" instead nothing happens. I don't believe the website is configured to work with JSONP either.
I need a way to figure out how to pull this data from the website at run time. Does anyone have any solutions or workarounds?
p.s. Is this something that might need to be sorted out on the other end? The people who own the website set this scenario up to be like a fake api call like typically you would pass in parameters to get back the specific information that you would need. In the case of this endpoint or url it just returns a single record or the file just contains the data listed above. They would like me to extract the data from their url at runtime.
You can't make a normal ajax call to to this other domain due to same origin policy.
You can use JSONP to load the remote page, but looking at that example output you wouldn't be able to access the data unless the remote site is setup for JSONP (assigning the JSON to a variable, calling a callback function, etc).
You could create a server-side passthrough script of your own. You don't mention what server-side technology you have available, but if you can use PHP, you do a passthrough like this:
<?php
echo file_get_contents("http://www.website.com/data.jsp");
?>
PHP (or any other server-side language) can fetch the remote data, and now you can use ajax to call your own script (which works since you're on the same domain).

Is there an online tool that grabs XML from some remote URL then provides a way to get the data via json using javascript?

Its easy to grab XML directly of your own domain from some local URL, but cross-domain isn't. How would you grab the XML data that is found at http://google.com/complete/search?output=toolbar&q=keep+skat with javascript?
Yes, you can! I would use YQL (Yahoo Query Language) and its pretty simple. You can definitely convert from XML to JSON and also can pass callback function to use along with JSONP.
Please visit YQL console and you can build your query from there.
For your problem, use this REST call (which I used to generate from YQL console):
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22http%3A%2F%2Fgoogle.com%2Fcomplete%2Fsearch%3Foutput%3Dtoolbar%26q%3Dkeep%2Bskat%22&format=json&diagnostics=true&callback=myCallbackFunc
Response from this URL will give you the JSON from the XML returned from your URL.

Categories

Resources