【问题标题】:Jquery .getJson() unexpected token :Jquery .getJson() 意外令牌:
【发布时间】:2015-12-27 09:06:00
【问题描述】:

当我尝试访问以下网址时:

http://dhclinicappv2stg.item-soft.co.il/LinkCareAppService.svc/json/GetFinalReportToAccount?AccountId=123

我收到 JSON 服务响应。我正在尝试通过 Jquery 阅读此响应,如下所示:

   (function()
      {
         var serviceAPI = "http://dhclinicappv2stg.item-soft.co.il/LinkCareAppService.svc/json/GetFinalReportToAccount?AccountId=?";
         $.getJSON( serviceAPI,
        {
          AccountId: "123"
         })
         .done(function( data )
         {
           alert(data);
         });
        })();
<!doctype html>
    <html lang="en">
      <head>
         <meta charset="utf-8">
         <title>jQuery.getJSON attempt</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      </head>
    <body>
 
    <div id="data"></div> <!-- in the future, I will add the data to this div -->
 
    <script>
   
      </script>
 
      </body>
    </html>

但我得到一个空白页,没有任何警报。控制台日志读取标题中的错误。

我做错了什么?

【问题讨论】:

  • 您提供的网址返回 xml 而不是 json。
  • @Sirko 那我怎么参考呢?
  • @T.J.Crowder 这些是参数。 nosession 是一个叫做“Message”的参数,true 是一个叫做“Error”的参数。在控制台中,我可以看到它们配对为 JSON
  • @A.Abramov:我只是查看了发回内容的来源。 Sirko 说得对,它是 XML,而不是 JSON。
  • @A.Abramov,您的服务 url 对于 getJSON 方法是错误的。看看我的回答!

标签: jquery json ajax web-services


【解决方案1】:

服务没有问题。由于此服务调用如下触发,您的服务 URL 删除 ?AccountId=? 唯一错误。

/GetFinalReportToAccount?AccountId=jQuery21101485728188417852_1451208967751&amp;AccountId=123&amp;_=1451208967752

尝试下面的工作 sn-p where parameter removed in URL for getJSON 方法。

(function() {
   var serviceAPI = "http://dhclinicappv2stg.item-soft.co.il/LinkCareAppService.svc/json/GetFinalReportToAccount";
   $.getJSON(serviceAPI, {
           AccountId: "123"
       })
       .done(function(data) {

          $('#data').text(JSON.stringify(data));
       });
})();
    <!doctype html>
        <html lang="en">
          <head>
             <meta charset="utf-8">
             <title>jQuery.getJSON attempt</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          </head>
        <body>
     
        <div id="data"></div> <!-- in the future, I will add the data to this div -->
     
          </body>
        </html>

【讨论】:

  • 同样重要的是要注意它返回的是 XML,而不是 JSON。而对于潜伏者来说,这只是因为端点支持 CORS(显然来自任何来源)。
猜你喜欢
  • 2015-06-09
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多