K
K
kish2014-04-25 21:48:01
JavaScript
kish, 2014-04-25 21:48:01

How to solve jsonp problem?

Task: force js to drive the value of the city into the region variable.

In this form it gives the following error:

SyntaxError: missing ; before statement


What could be the problem? Previously, the link ru.smart-ip.net/geoip-json worked fine , but the service died.

Now replacing the link does not fix the issue.

<script type="text/javascript">
$(function() {
var link2 = "http://geoip.elib.ru/cgi-bin/getdata.pl?fmt=json";
$.ajax({
      type: "GET",
      url: link2,
     
      crossDomain: true,
      dataType: "jsonp",
      success: function(res) {
      var region = res.Town;
      console.log(region);


}


});
});



</script>


Tried with another service htmlweb.ru/geo/api.php?json&ip

Same problem.

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
subalex, 2014-04-26
@subalex

You request data in JSONP format, and it comes in JSON.
Or find a service that can send data in JSONP, or you need to do a padding. Because cross-domain ajax is prohibited, except for JSONP.
In PHP, such a padding is 1 line of code:
Your JS code will look like this:

<script type="text/javascript">
$(function() {
var link = "линк на прокладку";
$.getJSON(link, function(res) {
      var region;
    
  for (var ip in res)
    region = res[ip].Town;
    
      alert(region);
});
});
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question