Weatherbug REST JSON - javascript

I have a solid understanding of HTML and javascript. I have no experience with JSON. I would like to get the data from the URL http://i.wxbug.net/REST/Direct/GetUv.ashx?zip=21044&api_key=vxwdyz3evgtvuv9d5e53sckc and display it on a webpage. I have looked around for a simple tutorial that explains how to retrieve JSON data from a URL without prevail. Can someone point me in the right direction?

If your page is in different domain, you will encounter the "...not allowed by Access-Control-Allow-Origin" error when you use javascript to access it. However, you could make a proxy page in the server side, fetch the data and then output data.
And then, you could get the data, for example, if you use jQuery, it is very easy like:
$.getJSON(your_proxy_page_url,function(data) {
console.log(data);
});

You could also try to take look at this post, maybe it can help you out:
http://docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getScript

Related

JavaScript GET to retrieve one variable

I haven't really written any javascript but am building an iOS application that will utilize JavaScriptCore's framework to read a javascript code to get a variable. What I'm looking to do is set up a GET (I think) so that I can retrieve JSON data from a url and then pull a specific string from the JSON data. Within the GET method, I'll need to add credentials and one parameter. What is the best practice to do this?
As per Rory's statement above the server you are requesting the json data from must either be on the same domain as your Application/Js code or support the CORS headers.
If the above is true, then you can either use JQUery as suggested above, or for a more minimalist approach the W3Schools has a tutorial on basic Ajax.
https://www.w3schools.com/xml/dom_httprequest.asp

I need to make a PUT request to a JSON file in an external API. I need to update specific fields

I have a JSON on an external API, and, in the documentation of the API, it instructs to use a PUT request to update records. I have read a lot on this, but I haven't found an adequate amount of information.
I will do my best to provide you with the most info I can so you can help me, specifically.
I have the url stored in a variable: $record.
The documentation instructs to: Set the request body content as a json formatted array of record data. This is the “payload”.
It also provides an example:
{
“Name”: “Craig J. Peters”,
“Job Title”: “Director of Engineering”
}
I need to change specific fields. How can I accomplish this using cURL and PHP?
I should mention: If this is easier with JavaScript, I am open to that as well.
I strongly recommend you to check Guzzle as a starting point -
http://docs.guzzlephp.org/en/latest/request-options.html#body

Is there a way I can retrieve the JSON file from server

Referring to my question above, the reason I want to retrieve the file is that I want to know how it is structured so I can make the script that I copied work using my data instead.
Currently, the $.getJSON url includes the string "callback=?" in it.
So is there anyway, I can peek through or see how the data is structured in the file thats inside the $.getJSON url string?
actually, Im following along a demo below
https://code.google.com/r/kgraham-flr-map/source/browse/examples/medicare-dynamic-ds.js?spec=svn72fd5de93cf24b0b2baa5d2678d9518741e3d80b&r=72fd5de93cf24b0b2baa5d2678d9518741e3d80b
Tthe script in question has the $.getJSON('https://storelocator-go-demo.appspot.com/query?callback=?' to parse the data.
When I tried opening the url, I get this error strconv.ParseFloat: parsing "": invalid syntax
I want to see how the JSON file is structured so I can replace it with my own data instead.
Hope this clears my question.
From the comments:
so you mean the error that I got when trying to open the json file is due to a server problem?
Well, it could give you a more useful error message. The problem is that you're not giving it the arguments it requires.
If I go to:
https://storelocator-go-demo.appspot.com/query?callback=foo
...then like you, I get that error. But if I give it arguments (these are cribbed from the examples link you gave):
https://storelocator-go-demo.appspot.com/query?callback=foo&lat=-29.292219923932738&lng=137.763512&n=-10.691329506145314&e=167.382652625&s=-47.89311034172016&w=108.14437137499999&audio=&access=&_=1364633286163
I get back a valid JSONP response.

POST to REST API from webpage with javascript

I am trying to post a JSON data struct to a REST API service from a webpage from. On submitting the form, javascript will capture form field values, make JSON data, then POST the data to my REST API. I am not entirely sure how to do this with JavaScript. Can anyone point me in the right direction or drop some sample code.
Thanks
Try jQuery, it's got some nice helpers for this kind of stuff.
Check out the jQuery.post documentation, you probably want the postJSON method.
As for getting started with jQuery, check out their getting started page
You can get a form's data with
var data = $('#formid').serializeArray();
Then you can use that in the postJSON method:
$.post(url, data, callback, "json");
Or for even easier solution, use my jQuery plugin: https://github.com/jpillora/jquery.rest
I need to POST to a different domain than my webpage will be on. Can I do this?
If you don't care about older IE's you can use headers to allow cross domain requests: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

JavaScript, JSONP and reading XML from cross-domain

in my JS project I need to load data from cross-domain. (JavaScript sits on domain A, the data comes from domain B)
I have a solution that uses JSONP but I really need to load an XML instead (ordinary XML music playlist). The main goal is to be able to load and parse the XML data without the need to modify them first to some other format (like JSONP).
Is it completely impossible? Or are there any workarounds or hacks?
I am targeting mainly the latest browsers mainly on iOS.
Thanks!
PS: Could easyXDM be of any help? Or it's not relevant to XMLs?
UPDATE: unfortunately I can not use proxy, I am really asking about a direct solution.
You can totally do this, just have your domain B return something like
func("<myxml></myxml>");
or
var someVar = "<myxml></myxml>";
The name JSONP doesn't really have anything to do with JSON specifically since its concept is all about executing JavaScript that has your data embedded in the code.
Once your domain B returns exactly one of those 2 forms above, domain A can simply use it either by doing:
<script>
function func(xmlString) {
alert(xmlString); // you can parse the xmlString with
// jQuery or something else
}
</script>
or if you use the second example:
<script>
alert(someVar);
</script>
The usual solution is to have a "AJAX proxy" - a simple server-side script running on your domain, that fetches the data from the other domain and returns it unchanged.
The simplist is to give the script the URL you need the data from:
http://example.com/proxy.php?url=http%3A%2F%2Fexample.org%2Fajax%3Fid%3D123 gets the data from http://example.org/ajax?id=123
This can however be misused if you let any URL be fetched like that, so you should have your script, check that it actually only gets data from a specific URL.
In order to avoid having to parse the URL to check this, you could write a proxy specificly for your app, that only accesses the specific resource you need:
http://example.com/proxy.php?id=123 to access http://example.org/ajax?id=123.
If you have a JSON-P solution in place, you can just pass the XML to the JSON-P callback as a string. You can then do XML parsing of a variable string in JavaScript
The whole idea with JSONP is that the response must be executable as script. So sure, you can pass XML data back, as long as it's valid Javascript - for example, the server could wrap its response in a string:
myCallback('<xml><stuff/></xml>')
and you'd have to parse it with jQuery:
success: function(data) {
var xml = $(data); // now do stuff
}
This assumes that you control the other server and/or someone who does is interested in formatting their data that way. Otherwise, you're out of luck, and need a proxy of some sort - you might be able to do this with YQL.

Categories

Resources