【问题标题】:Pulling json data from a web server into text将 json 数据从 Web 服务器拉入文本
【发布时间】:2012-12-19 18:45:06
【问题描述】:

大家好,我在这个问题上纠结了一段时间。我目前正在使用 dojo.xhrget 将 json 数据作为文本放到我的 html 页面上。目前我在 safari 上不断收到错误,我相信由于跨域问题,它似乎没有响应其他浏览器 -

" An unexpected error occurred: RequestError: Unable to load 171.51.21.41:3000/information.json status: 0"

我尝试使用 dojo.io.script,但它仍然是空白的。我怀疑由于某种原因它无法读取我的 json 数据 - 任何帮助将不胜感激。

require(["dojo"], function(dojo){

dojo.ready(function(){
// Look up the node we'll stick the text under.
var targetNode = dojo.byId("licenseContainer");

// The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
var xhrArgs = {
url: "171.51.21.41:3000/information.json ",
handleAs: "json",
load: function(data){
  // Replace newlines with nice HTML tags.
  data = data.replace(/\n/g, "<br>");

  // Replace tabs with spaces.
  data = data.replace(/\t/g, "&nbsp;&nbsp;&nbsp;");

  targetNode.innerHTML = data;
},
error: function(error){
  targetNode.innerHTML = "An unexpected error occurred: " + error;
}
}

 // Call the asynchronous xhrGet
 var deferred = dojo.xhrGet(xhrArgs);
});

});

我借用了这段代码并进行了编辑。我知道错误语句是由于错误而出现的 功能;但我想知道为什么以及如何访问 json。

先谢谢了~

我的 JSON 片段:

[{"id":1,"Field":"Devices","Phase":"Final","Name":"MP3 Player"}]

【问题讨论】:

  • 您是否尝试从与您所在的域不同的域中获取数据?
  • 你的 information.json 是一个有效的 json 文件吗?
  • 你需要把http://放到你的xhrArgs url吗?
  • 啊!非常感谢
  • @PatrickGunderson 也许您应该将其发布为答案,以便他可以接受...

标签: javascript json dojo get


【解决方案1】:

您需要将http:// 放入您的 xhrArgs 网址中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2016-12-26
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多