2010-11-20 61 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