doulin1867 2012-09-20 15:12
浏览 87
已采纳

在绑定事件(XHR)中嵌入Ajax请求

I'm trying to find the answer somewhere around but i can't,

So i'm having a .bind() event and I want when's triggered to make a get query from a php file with JSON like AJAX does.

I have tried the following which doesn't work:

$(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();
};
});
  • 写回答

2条回答 默认 最新

  • dongnao2582 2012-09-20 15:22
    关注

    It looks like you're using jQuery, so this should work:

    $(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);
                }
            });
        });
    });
    

    There's already an awesome $.getJSON method in jQuery that will do all of the heavy lifting for you, including return your results as JSON.

    You'll need to make your getdates.php script echo the callback and wrap your results in parentheses so it returns as actual jQuery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格