我试图在某个地方找到答案,但我不能,
所以我有一个 .bind() 事件,我希望何时触发以像 AJAX 一样使用 JSON 从 php 文件进行获取查询。
我已经尝试了以下不起作用的方法:
$(document).ready(function() {
$("#adivhere").bind("valuesChanged", function(){
// Some variables here
var max2 = 10
var min2 = 5
//The classic AJAX Request
function afunc(max2,min2){
var xmlhttp;
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
//The classic AJAX onreadystatechange function
xmlhttp.onreadystatechange=function()
{ if (xmlhttp.readyState==4 && xmlhttp.status==200){
//The code triggered
var fets = jQuery.parseJSON( xmlhttp.responseText );
var t = fets.date.split(/[-]/);
var d = new Date(t[0], t[1], t[2]);
alert(d);
}}
//The XHR request with the .php file and the
// two values that sends
xmlhttp.open("GET","getdates.php?max2="+max2+"&min2="+min2,true);
xmlhttp.send();
};
});
看起来你正在使用jQuery,所以这应该可以工作:
$(function(){
$('#adivhere').bind('valuesChanged',function(){
var max2 = 10;
var min2 = 5;
$.getJSON('getdates.php?callback=?', {max2: max2, min2: min2}, function(json){
alert(json);
var t = json.date.split(/[-]/);
if (t && t.length) {
var d = new Date(t[0], t[1], t[2]);
alert(d);
}
});
});
});
jQuery中已经有一个很棒的$.getJSON方法,它将为你完成所有繁重的工作,包括将结果作为JSON返回。
您需要使getdates.php
脚本回显callback
并将结果括在括号中,以便它作为实际的jQuery返回。
脚本中有几个"错误":
- "afunc"永远不会被调用,那么为什么要执行它呢?
- 括号没有正确闭合;末尾缺少两个:)}