вот мой код:
<script src="/jquery.min.js"></script>
<?php
$url = "http://api.wunderground.com/api/7d5339867b063fd0/geolookup/conditions/q/UK/".$area.".json";
?>
<script>
jQuery(document).ready(function($) {
$.ajax({
url : ,
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
alert("This is an example of a web service outputting using Json. The current temperature in " + location + " is: " + temp_f);
}
});
});
</script>
Как я могу добавить переменную "$ url" в код javascript в месте "url:" здесь ",
благодаря
Вам просто нужно сказать следующее:
url : "<?=$url?>",
ИЛИ (лучший способ):
url : "<?php echo $url; ?>",
<script src="/jquery.min.js"></script>
<?php
$url = "http://api.wunderground.com/api/7d5339867b063fd0/geolookup/conditions/q/UK/".$area.".json";
?>
<script>
jQuery(document).ready(function($) {
$.ajax({
url : "<?=$url?>",
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
alert("This is an example of a web service outputting using Json. The current temperature in " + location + " is: " + temp_f);
}
});
});
</script>
"<?=$url?>"
Использовать json_encode()
:
var jsVar = <?=json_encode($phpVar)?>;
или в вашем случае:
$.ajax({
url: <?=json_encode($url)?>,
dataType: "jsonp"
// ...
});
Это самый безопасный способ и работает с любыми данными (массивами и т.д.).
url : "<?php print $url ?>",
http://example.com/xxx?var=foo"bar