从外部URL从JSON数组中获取数据 [英] Get data out of JSON array from external URL

查看:145
本文介绍了从外部URL从JSON数组中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JSON比较陌生.我已经阅读了本教程并尝试实施它,但是没有运气. 基本上我有一个提供JSON数据/提要的外部URL.数据为数组形式.现在,我正在尝试编写一个JavaScript程序(在本地),该程序将从URL中获取数据并将其放入html中.

I am relatively new to JSON. I have read the tutorial and trying to implement it but no luck. Basically I have an external URL that gives JSON data/feed. The data is in the form of array. Now I am trying to write a JavaScript Program (on my local) that would get the data out of this URL and would put in my html.

这是功能.它还包括外部链接. 但是我没有任何结果.只是空的. 我错过了什么还是做错了什么?

Here is the function. It includes the external link also. But I am not getting any result. Just empty. Am I missing something or what I am doing wrong?

 <!doctype html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>Index Page</title>
 </head>

 <body>
 <div id="id01"></div>

 <script>
  var xmlhttp = new XMLHttpRequest();
  var url = "http://mpatrizio-001-site5.smarterasp.net/categoryList.php?D=B7ACEF70-4901-41C8-930F-D4D681D82DAA";

  xmlhttp.onreadystatechange = function() {
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
         var myArr = JSON.parse(xmlhttp.responseText);
         myFunction(myArr);
     }
 }
 xmlhttp.open("GET", url, true);
 xmlhttp.send();

 function myFunction(arr) {
     var out = "";
     var i;
     for(i = 0; i < arr.length; i++) {
         out += arr[i].CategoryID + '<br>';
     }
     document.getElementById("id01").innerHTML = out;
 }
 </script>     
 </body>
 </html>


更新:

在你们向正确的方向指出(非常感谢)之后,我发现由于某些CORS错误,服务器正在阻止该请求.我正在研究. 请查看以下控制台中显示的错误图片. 从中,您能具体指出解决方案吗?

After being pointed in the right direction by you guys (thank you very much for that), I have found that the request is being blocked by server due to some CORS error. I am studying it. Please review the following image of the error I got in the console. From it, can you specifically point out the solution?

推荐答案

在chrome的exe属性中在路径前添加--disable-web-security(在路径C; ... \ chrome.exe中).

Append --disable-web-security (at path C;...\chrome.exe) in chrome's exe properties preceded by a space.

更优雅的解决方案:

其他解决方案将在服务器端.这将在服务器上创建crossdomain.xmlclientaccesspolicy.xml文件.它的结构就像:

Other solution will be on server side. Which is to create crossdomain.xml and clientaccesspolicy.xml file on server. It's structure is like:

crossdomain.xml:

crossdomain.xml:

<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
</cross-domain-policy>

clientaccesspolicy.xml:

clientaccesspolicy.xml:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="SOAPAction">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

其中一些教程是:

http://help.adobe. com/en_US/AS2LCR/Flash_10.0/help.html?content = 00000469.html

http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html

它的规格是:

http://www.adobe.com/devnet- docs/acrobatetk/tools/AppSec/CrossDomain_PolicyFile_Specification.pdf

其他教程:

https://msdn.microsoft.com /en-us/library/cc197955(v=vs.95).aspx

这篇关于从外部URL从JSON数组中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆