现在的位置: 首页 > 综合 > 正文

$.ajax()例子

2013年10月17日 ⁄ 综合 ⁄ 共 836字 ⁄ 字号 评论关闭

下面是一个使用ajax的例子,具体功能是对“grammerValue”编辑框里的内容(一小段代码)发送到服务器,进行语法检查。

其中url是发送请求的地址。

else if( dataElementType.typeName === "grammar" ){
	var grammarValue = editAreaLoader.getValue("grammarValue");
			
	var valid = false;
	// validate
	$.ajax({
		type: 'GET',
		async: false,
		contentType: 'application/json',
		url: "${pageContext.request.contextPath}/admin/qry/testScript?script="+encodeURIComponent(grammarValue),
		dataType: "json",
		data:  JSON.stringify(values),
		success: function(data){
			valid = true; 
		},
		error: function(e){
			valid = false;
			$("#defineSystemFieldMsg").text("Syntax error! "+ e.statusText);
			$("#defineSystemFieldMsg").show();
		},
		complete: function(){
		}
	});		
			    
	if(!valid)
		return;
			
	var sysValue = new SystemDataElementValue(1,grammarValue);
	if(!grammarValue || "" === grammarValue ){
		$("#defineSystemFieldMsg").text("You must enter the grammar syntax");
		$("#defineSystemFieldMsg").show();
		return;
	}
	values.push(sysValue);
}

抱歉!评论已关闭.