这是我的代码:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/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”变量添加到位于“url:”此处的javascript代码中,
谢谢
答案 0 :(得分:3)
你只需要这样说:
url : "<?=$url?>",
或(最佳方式):
url : "<?php echo $url; ?>",
答案 1 :(得分:3)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/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>
答案 2 :(得分:1)
url : "<?php print $url ?>",
答案 3 :(得分:1)
使用json_encode()
:
var jsVar = <?=json_encode($phpVar)?>;
或在你的情况下:
$.ajax({
url: <?=json_encode($url)?>,
dataType: "jsonp"
// ...
});
这是最安全的方法,适用于任何数据(数组等)。