Jquery中的ajax的使用

本文介绍如何使用 jQuery 的 AJAX 功能进行前后端交互,包括 GET 和 POST 方法的应用,并提供了一个使用 PHP 处理 JSON 数据的具体实例。

ajax使用很简单,但有时候很麻烦。

因为不是很专业,所以大神告诉我用jquery,.....确实方便

为什么不早告诉我.......

$.ajax({
		type: "GET",
		//method: "Get",
		dataType: "json",
		data:{"radio": radiovalue,"imageid":$('#myimage').attr('value')},
		url: "Save.php",
		success: function(data){
			window.location.reload();
		}
	});

以上是一段javascript的代码

曾是jquery中使用ajax的固定格式

其实大家只需要注意几个选项就可以了

url:这个是需要调用的文件,具体是说明文件就看大家喜好了

type:发送数据的方式,一般有get和post两种

datatype:接受数据发送数据的格式,json比较好,不过其他也是可以的

data:这个是发送给url文件的数据,这里我们写成json的格式

success:意思下接受到url文件处理数据后需要执行的程序,卸载function中就可以了。

这里给出一个php中接受处理数据的程序,帮助大家理解:

<?php

$m = new Mongo();

// select a database
$db = $m->FourS2;

// select a collection (analogous to a relational database's table)
$collection = $db->AnnotationImage;
$imagelist = $db->imagelist;

$value = $_GET['radio'];
$imageid = $_GET['imageid'];

$query = array("_id"=>$imageid);
$imageinf = $imagelist->findOne($query);
$imageinf['flag'] = $value;

$collection->save($imageinf);
$imagelist->remove($query);

?>

大家可以看到,其中

$value = $_GET['radio'];
$imageid = $_GET['imageid'];

为接受到的由javascript发送来的数据

这里没有返回的数据

我们一般使用echo就可以了

但这我们需要注意,如果你使用的是json各式,需要一个编码的过程,如下程序所示:

$result = json_encode($temp);

echo $result;

这样,得到的数据就是json各式的了。

javascript处理json各式就不用说了

大牛们一定都很熟悉了。

下面给出一个例子,

这里我们使用php读取mongodb的数据然后传递给javascript,

javascript顺序读取php传递过来的数据:

javascript的代码如下:

function getServerData(){
	$.ajax({
		url:"GetVenueData.php",
		//data:"name=周&sex=男&age=11",
		//timeout:3000,
		type:"POST",
		error:function (XMLHttpRequest, textStatus, errorThrown) {},
		beforeSend:function (XMLHttpRequest) {},
		success: function (data, textStatus) {},  
		complete:handleResponse
	}); 
}
function handleResponse(XMLHttpRequest, textStatus) {
	//alert('...'+XMLHttpRequest);
	var latlng = new google.maps.LatLng(sgLat, sgLng);   
	var myOptions = {zoom: 13,       
					  center: latlng,      
					  mapTypeId: google.maps.MapTypeId.ROADMAP};     
					
	map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
	
	myjson = eval(XMLHttpRequest.responseText);
	
	drwointilize();
}

这里myjson就是我们读到的一系列的json数据

之后用下面的程序循环读取:

function drwointilize() { 

	alert('hello');
	var str="";
	var center = map.getCenter();
	//alert(center);
	for(var i=0;i<myjson.length;i++){
	
		//alert(myjson[i].lat);
		//var center = map.getCenter();
		var myLatlng = new google.maps.LatLng(myjson[i].lat,myjson[i].lng);
		var distance = google.maps.geometry.spherical.computeDistanceBetween(center, myLatlng);
		var northeastlat = map.getBounds().getNorthEast().lat();
		var southwestlat = map.getBounds().getSouthWest().lat();
		var northeastlng = map.getBounds().getNorthEast().lng();
		var southwestlng = map.getBounds().getSouthWest().lng();
		//alert(northeastlat);
		//alert(southwestlat);
		//alert(myjson[i].lat);
		if(myjson[i].lat<northeastlat&&myjson[i].lat>southwestlat&&myjson[i].lng<northeastlng&&myjson[i].lng>southwestlng){
			var countf = 300;
			var populationOptions = {
					strokeColor: '#FFFFFF',
					strokeOpacity: 0.8,
					strokeWeight: 2,
					fillColor: '#FF0000',
					fillOpacity: 0.35,
					map: map,
					center: myLatlng,
					radius: countf
				  };
			cityCircle = new google.maps.Circle(populationOptions);
			cityCircle1s.push(cityCircle);
			//alert(myjson[i]["Photos"]["1"]["Url"]);
			//alert(myjson[i]["Title"]);
			str = str+"<img onclick='aaa(this)' name = '"+ myjson[i]["Title"]+"' title='" + myjson[i]["Venue_id"] + "' src=\"" + myjson[i]["Photos"]["1"]["Url"] + "\" width=\"100\" height=\"100\">";
		}
	}


里面有google map的函数,大家可以删除了去看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值