0

How I can parse parameters like "city" from json?

I have url like this: http://api.db-ip.com/addrinfo?addr=8.8.8.8&api_key=key, this return me this:

{
    "address":"8.8.8.8",
    "country":"US",
    "stateprov":"California",
    "city":"Mountain View"
}

I want to apply city (or country) variable to <input type>, to show visitors some info about location.

3
  • 3
    is db-ip.com your domain or is it an external resource? Commented Sep 16, 2013 at 15:14
  • Can you check what errors are there in the console (F12). If I'm correct, there should be an error saying that you can't do cross-domain AJAX. db-ip doesn't seem to have CORS nor JSONP. Commented Sep 16, 2013 at 15:57
  • Yes, I get this error: is not allowed by Access-Control-Allow-Origin. Commented Sep 16, 2013 at 16:16

2 Answers 2

1

If you just want to load that JSON object and access the "city" field, use jQuery's getJSON method to get a native JS object in response.

$.getJSON( "http://api.db-ip.com/addrinfo?addr=8.8.8.8&api_key=key", function(data) {
   // do something with data.city;
});

However, in your case, you're trying to make a cross-domain JSON request -- not allowed. Since db-ip.com doesn't allow it, you'll have to proxy the request using PHP.

Set up "dbip.php" on your server as a proxy (cf. http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html)

Then you just load JSON from dbip.php on your own server, which queries db-ip.com for you. The browser is happy because the AJAX request it makes doesn't cross domain names.

Sign up to request clarification or add additional context in comments.

9 Comments

I'm trying to get city value from this json in to input field (for form).
Does your input field have a specific ID? e.g. <input id="city"/> If so, you'd just do something like $('#city').val(data.city) inside that function block.
Yes, I've trying this: ` <input type="text" id="city"/> <script type="text/javascript"> $.getJSON( "api.db-ip.com/addrinfo?addr=8.8.8.8&api_key=key", function(data) { $('#city').val(data.city); }); </script> `
But can't get value city.
I just noticed the comment above (that you're loading cross-domain AJAX). You can't make a direct JSON request from another domain. JSONP is a simple cross-browser solution (loading a script tag that contains the desired response, wrapped in a call to your chosen method) if there was a supported JSONP endpoint at db-ip.
|
0
var response={
 "address":"8.8.8.8",
 "country":"US",
 "stateprov":"California",
 "city":"Mountain View"
};
$("#city").val(response.city);

5 Comments

Cool, but how I can get 'responce' from url?
if url return type is json ,you can get that with ajax.
Could you please tell me how I can to this?
i think you are running html on file system. you should try localhost or web.
No, I run this from running web server. I mean, your example works perfect. But in my case (using dp-ip.com) I have cross-domain issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.