Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery getJSON不使用输入按钮_Jquery_Jquery Selectors - Fatal编程技术网

Jquery getJSON不使用输入按钮

Jquery getJSON不使用输入按钮,jquery,jquery-selectors,Jquery,Jquery Selectors,如果我有a hrefgetJSON按预期工作正常(获取警报消息框),但如果我使用input按钮,则它不工作(没有警报消息),下面是我使用a href和input按钮的示例,我打开firebug,没有看到任何数据响应 工作 <a href="'http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?">GetCustomerBy</a> $(function () {

如果我有
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);
        });
    });

$(函数(){
$('a')。单击(函数(){
$.getJSON(this.href,{id:'2'},函数(customer){
警报(客户名称);
警报(客户地址);
});
返回false;
});
});
不工作

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

$(“#驱动程序”)。单击(函数(事件){
$.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?“,{id:'2'},函数(客户){
警报(客户地址);
警报(客户名称);
});
});
试试这个:

$(function() {
    $('#driver').click(function() {
        $.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?', { id: '2' }, function (customer) {
            alert(customer.Address);
            alert(customer.Name);
        });
    });
});
请注意,调用被包装在$(document).ready中,并且不需要
返回false
。也不需要对匿名回调使用
事件
参数

此外,您的href地址与您在按钮中使用的地址不同。在href中,您有:

http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?
http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?
而在按钮中,您有:

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

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

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

$(“#驱动程序”)。单击(函数(事件){
$.getJSON('http://host/Myservice.svc/GetCustomerBy“,{id:'2'},函数(客户){
警报(客户地址);
警报(客户名称);
});
});
这将导致URL看起来像
http://host/Myservice.svc/GetCustomerBy?id=2