getJSON 不适用于输入按钮

发布于 2024-10-03 08:43:25 字数 1010 浏览 1 评论 0原文

如果我有 a href getJSON 按预期工作正常(收到警报消息框),但是如果我使用 input 按钮 那么它就不起作用(没有警报消息),下面是我使用 a hrefinput 按钮 的示例,我打开 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-10-10 08:43:25

试试这个:

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

请注意,调用包装在 $(document).ready 中,并且您不需要返回 false。也无需使用匿名回调的 event 参数。

另外,比较您的 href 地址与您在按钮中使用的地址不同。在 href 中你有:

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

而在按钮中你有:

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

这是不一样的。因此,无论该地址是什么,请确保使用正确的地址。 FireBug 在这种情况下会对您有所帮助。

Try this:

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

Notice that the call is wrapped in a $(document).ready and that you don't need to return false. Also no need to use the event 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:

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

whereas in the button you have:

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

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.

此岸叶落 2024-10-10 08:43:25

您的网址看起来不正确——太多了?和 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);
    });
});

这会导致 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:

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

That would result in a URL that looks like http://host/Myservice.svc/GetCustomerBy?id=2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文