jquery无法按照文档进行工作

jquery无法按照文档进行工作,jquery,Jquery,这是我的密码 更改下拉列表时,我会收到关于呼叫服务的警报消息。然后,当我看到浏览器的网络工具时,我还可以看到根据下拉列表中的选择调用json url。这意味着在调用url之前,一切都正常。但是,返回的警报数据不会被调用,结果div层也不会从调用的json url中获取值。根据这份文件,我没有做错什么。那么到底发生了什么?可能是您的URL有问题,根据您的代码,您似乎正在访问本地Json文件。如果是这样,请尝试使用您的文件夹结构,而不是'http://localhost:10304/jsondata

这是我的密码


更改下拉列表时,我会收到关于呼叫服务的警报消息。然后,当我看到浏览器的网络工具时,我还可以看到根据下拉列表中的选择调用json url。这意味着在调用url之前,一切都正常。但是,返回的警报数据不会被调用,结果div层也不会从调用的json url中获取值。根据这份文件,我没有做错什么。那么到底发生了什么?

可能是您的URL有问题,根据您的代码,您似乎正在访问本地Json文件。如果是这样,请尝试使用您的文件夹结构,而不是'http://localhost:10304/jsondata/1.json“。

可能是您的URL的问题,根据您的代码,您似乎正在访问本地Json文件。如果是这样,请尝试使用您的文件夹结构,而不是'http://localhost:10304/jsondata/1.json’。

这可能无法为您提供有效的解决方案,但肯定会帮助您评估问题

将fail处理程序附加到ajax调用中,看看它是否返回了什么

$.get(urlString, function (data)
{
     alert('data returned: ' + data);
     $( "#results" ).html( data );
     alert( "Load was performed." );
}).fail(function(jqXHR, textStatus, errorThrown) {
    alert( "Ajax Error: "+textStatus );
});
有关这方面的更多信息可以找到


此外,如果存在相同的错误,也应在浏览器控制台中显示。

这可能无法为您提供有效的解决方案,但肯定会帮助您评估问题

将fail处理程序附加到ajax调用中,看看它是否返回了什么

$.get(urlString, function (data)
{
     alert('data returned: ' + data);
     $( "#results" ).html( data );
     alert( "Load was performed." );
}).fail(function(jqXHR, textStatus, errorThrown) {
    alert( "Ajax Error: "+textStatus );
});
有关这方面的更多信息可以找到


此外,如果存在相同的错误,也应在浏览器控制台中显示。

您从URL返回什么,请查看以下示例

 $("#theOption").change(function ()
    {
        var urlString = '';

        if (this.value == 1) {
            urlString = 'http://ip.jsontest.com/';
        }
        else {
            urlString = 'http://ip.jsontest.com/';
        }



        alert("About to call service." );
        $.get(urlString, function (data)
        {
            alert('data returned: ' + data.ip);
            $( "#results" ).html( data.ip );
            alert( "Load was performed." );
        });
    });

你从URL返回什么,看看下面的示例

 $("#theOption").change(function ()
    {
        var urlString = '';

        if (this.value == 1) {
            urlString = 'http://ip.jsontest.com/';
        }
        else {
            urlString = 'http://ip.jsontest.com/';
        }



        alert("About to call service." );
        $.get(urlString, function (data)
        {
            alert('data returned: ' + data.ip);
            $( "#results" ).html( data.ip );
            alert( "Load was performed." );
        });
    });

我认为$.get用于从服务器端获取数据。因此,您无法从json获取数据。当我将1.jsom和2.json更改为php并在这些php文件中回显一个字符串时,效果很好

    <script src="jquery-1.8.1.js"></script>
<script>

    jQuery(document).ready(function ()
    {
        $("#theOption").change(function ()
        {
            var urlString = '';

            if (this.value == 1) {
                urlString = '1.php';
            }
            else {
                urlString = '2.php';
            }



            alert("About to call service." );
            $.get(urlString).done(function (data)
            {
                alert('data returned: ' + data);
                $( "#results" ).html( data );
                alert( "Load was performed." );
            });
        });


    });

</script>


<div class="row">
    Select
    <select id="theOption">
        <option value="1">one</option>
        <option value="2">two</option>
    </select>
</div>

<div id="results">


</div>
这就是我在php文件中所做的

<?php
echo "saasfafassfafafafsasf";
?>

我认为$.get用于从服务器端获取数据。因此,您无法从json获取数据。当我将1.jsom和2.json更改为php并在这些php文件中回显一个字符串时,效果很好

    <script src="jquery-1.8.1.js"></script>
<script>

    jQuery(document).ready(function ()
    {
        $("#theOption").change(function ()
        {
            var urlString = '';

            if (this.value == 1) {
                urlString = '1.php';
            }
            else {
                urlString = '2.php';
            }



            alert("About to call service." );
            $.get(urlString).done(function (data)
            {
                alert('data returned: ' + data);
                $( "#results" ).html( data );
                alert( "Load was performed." );
            });
        });


    });

</script>


<div class="row">
    Select
    <select id="theOption">
        <option value="1">one</option>
        <option value="2">two</option>
    </select>
</div>

<div id="results">


</div>
这就是我在php文件中所做的

<?php
echo "saasfafassfafafafsasf";
?>

可能是您的URL有问题,根据您的代码,您似乎正在访问本地Json文件。如果是这样的话,那么试着使用你的文件夹结构而不是。看起来你的url不正确可能是你的url的问题,根据你的代码,看起来你正在访问你的本地Json文件。如果是这样的话,那么试着用你的文件夹结构代替。你的url似乎不正确。我的json格式不正确。虽然我不明白{color:blue,value:somevar}与{color:blue,value:somevar}有什么不同,但我很感兴趣。我的json格式不正确。虽然我不明白{color:blue,value:somevar}如何与{color:blue,value:somevar}不同