Answer the question
In order to leave comments, you need to log in
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
<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>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question