getJSON 不适用于输入按钮
如果我有 a href
getJSON 按预期工作正常(收到警报消息框),但是如果我使用 input 按钮
那么它就不起作用(没有警报消息),下面是我使用 a href
和 input 按钮
的示例,我打开 Firebug,但没有看到任何响应数据。
工作
<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);
});
});
if i have a href
getJSON is working fine as expected (getting alert message box) but where as if i use input button
then it does not work (no alert message), below is my example using both a href
and input button
, i open the firebug and i dont see that any data in response.
working
<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;
});
});
not working
<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);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
请注意,调用包装在 $(document).ready 中,并且您不需要
返回 false
。也无需使用匿名回调的event
参数。另外,比较您的 href 地址与您在按钮中使用的地址不同。在 href 中你有:
而在按钮中你有:
这是不一样的。因此,无论该地址是什么,请确保使用正确的地址。 FireBug 在这种情况下会对您有所帮助。
Try this:
Notice that the call is wrapped in a $(document).ready and that you don't need to
return false
. Also no need to use theevent
argument to the anonymous callback.Also comparing your href address is not the same as the one you are using in the button. In the href you have:
whereas in the button you have:
which is not the same. So make sure you use the correct address whatever this address is. FireBug would have helped you in this case.
您的网址看起来不正确——太多了?和
GetCustomerBy
似乎是重复的。不应该是这样吗:这会导致 URL 看起来像
http://host/Myservice.svc/GetCustomerBy?id=2
Your URLs don't look right -- too many ? and
GetCustomerBy
seems to be repeated. Shouldn't it be:That would result in a URL that looks like
http://host/Myservice.svc/GetCustomerBy?id=2