2010-11-20 60 views
0

如果我有a href getJSON工作正常(获得警报消息框),但如果我使用input button然后它不起作用(没有警报消息),下面是我的示例使用a hrefinput button,我打开萤火虫,我没有看到任何数据的回应。getJSON不能使用输入按钮

工作

<a href="'http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?">GetCustomerBy</a> 


$(function() { 
      $('a').click(function() { 
       $.getJSON(this.href, { id: '2' }, function (customer) { 
        alert(customer.Name); 
        alert(customer.Address); 
       }); 
       return false; 
      }); 
     }); 

不工作

<input type="button" id="driver" value="Load Data" /> 

    $("#driver").click(function (event) { 

     $.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?', { id: '2' }, function (customer) { 

      alert(customer.Address); 
      alert(customer.Name); 
     }); 
    }); 
+0

两个URL是不同的 – 2010-11-20 20:13:24

回答

2

试试这个:

$(function() { 
    $('#driver').click(function() { 
     $.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?', { id: '2' }, function (customer) { 
      alert(customer.Address); 
      alert(customer.Name); 
     }); 
    }); 
}); 

注意,调用包装在$(文件)。就绪和你不需要return false。也不需要使用event参数来匿名回调。

同时比较您的href地址与您在按钮中使用的地址不同。在href您有:

http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=? 

而在按钮,您可以:

http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=? 

这是不一样的。所以请确保你使用正确的地址,无论这个地址是什么。 FireBug会在这种情况下帮助你。

+0

我错过了'$(文件)。就绪(函数URL(){.. .' – 2010-11-21 00:40:44

0

您的网址看起来不正确 - 太多?和GetCustomerBy似乎重复。它不应该是:

<input type="button" id="driver" value="Load Data" /> 

$("#driver").click(function (event) { 

    $.getJSON('http://host/Myservice.svc/GetCustomerBy', { id: '2' }, function (customer) { 

     alert(customer.Address); 
     alert(customer.Name); 
    }); 
}); 

这将导致看起来像http://host/Myservice.svc/GetCustomerBy?id=2