欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通过jQuery解决AJAX跨域问题 博客分类: jQuery  

程序员文章站 2024-03-19 12:38:28
...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Ajax跨域调用</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
</head>
<body>
<h1><a href=" http://www.skygq.com/2011/03/04/jquery-ajax跨域调用">jquery-ajax跨域调用</a></h1>

 <img id="ajax_image" src="http://www.skygq.com/ci/css/img/ajax_image.gif" style="position:absolute;z-index:10;display:none;" border="0"/>
<select id="letter2">
	<option value="">请选择首字母</option>
	<option value="A">A</option>
	<option value="B">B</option>
	<option value="C">C</option>
	<option value="D">D</option>
	<option value="E">E</option>
	<option value="F">F</option>
	<option value="G">G</option>
	<option value="H">H</option>
	<option value="I">I</option>
	<option value="J">J</option>
	<option value="K">K</option>
	<option value="L">L</option>
	<option value="M">M</option>
	<option value="N">N</option>
	<option value="X">X</option>
</select>
<select id="server2">
	<option value="">等待你选择首字母</option>
</select>

<script>
$(function(){
	$("#letter2").change(function(e){
		var $$ = $(this);
		if ($$.val() != ''){
			var $ajax_image = $("#ajax_image");
			var position = $$.offset();
			$ajax_image.css({"top":position.top,"left":position.left}).show();
			$("#server2").empty().append("<option value=''>正在调用数据...</option>");
			$.getScript("http://www.skygq.com/ci/jquery_ajax_json_php/search_server_2.html?letter="+$$.val(),function(){
				if (json.length > 0){
					var result = "";
					$.each(json,function(index,entry){
						result += "<option value='"+entry.id+"'>"+entry.name+"</option>";
					});
					$("#server2").empty().append(result);
				}else{
					alert("未找到数据!");
				}
				$ajax_image.hide();
			})
		}
	})
});
</script>
</body>
</html>