2016-11-11 49 views
0

工作这是我的代码以this version of jqueryjQuery的获得不是按照文件

<script> 

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

      if (this.value == 1) { 
       urlString = 'http://localhost:10304/jsondata/1.json'; 
      } 
      else { 
       urlString = 'http://localhost:10304/jsondata/2.json'; 
      } 



      alert("About to call service."); 
      $.get(urlString, 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> 

在下拉我得到的警报About to call service消息的变化。然后,当我看到浏览器的网络工具时,我还可以看到按下拉菜单中的选择调用json url。这意味着,直到调用url的部分,一切正常。但是,警报data returned不会被调用,结果div层也不会从调用的json url中获取值。根据本文件here,我没有做错任何事。那么发生了什么?

+0

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

+0

它似乎是你的网址不正确https://jsfiddle.net/7vbok82s/ –

回答

1

这可能不会给你一个工作解决方案,但肯定会帮助你评估问题。

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

有关这方面的更多信息可以发现here

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

+0

有趣。我的JSON格式不正确。虽然我不知道如何'{ \t “颜色”: “蓝”, \t “值”: “有些VAR” }'可以从不同的'{ \t颜色: “蓝”, \t值: “一些变种” }' – user20358

0

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

1

你这是从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."); 
     }); 
    }); 
0

我认为$不用彷徨是指从服务器端获取数据。所以你不能从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"; 
?>