使用jQuery刮取网页并获取JSON对象

使用jQuery刮取网页并获取JSON对象,jquery,ajax,Jquery,Ajax,有一个页面(来自同一个域),我正试图获取一个JSON,它位于标记内,带有一个特定的id <div id="someID"> <script type="text/json" id="scriptID"> { "some more here": "and there", "there are many more": "and the structure is various" } </script

有一个页面(来自同一个域),我正试图获取一个JSON,它位于
标记内,带有一个特定的
id

<div id="someID">
    <script type="text/json" id="scriptID">
      {
        "some more here": "and there",
        "there are many more": "and the structure is various"
      }
    </script>
  <script>
    console.log("just another script which can be with or without id")
  </script>
</div>
当我记录输出时,当我这样做时:

var newData = $(data).find("script").attr("id", "ceci-definition").text();
console.log(newData);
我看到的是第二个剧本的内容

    console.log("just another script which can be with or without id")
您对和的使用不正确

您要做的是查找
脚本
,并将其ID设置为
scriptID

试试这个:

console.log($(data).find("script#scriptID").contents());

基本示例:

您是否看到错误?发生什么事了?对不起,我忘了最重要的部分。当我执行<代码>那个元素。文本()>代码>时,我只看到第二个<代码> <代码>返回(那些控制台。log(…)),而不是我需要的那个。这非常重要:请考虑用这个信息编辑问题!(我已经尝试过了,但是返回jQuery对象是空的。代码>上下文:未定义的< /代码>编辑我的答案。如果这没有帮助,请考虑设置一个jsFoDelp来告诉我们发生了什么。我可以看到您的JSFIDLE在那里工作得很好。(不知道为什么我不能这么做。让我试一试,这可能也与你的HTML中有一个闭包<代码> <代码>有关:它可能过早地结束你的JavaScript。事实上,我很确定这是个问题。考虑把它包装在其他类型的标签中吗?观察我在注释HTML和删除的时候会发生什么吗?注释(此版本包含注释,删除注释,重新运行,并观察您的控制台)
console.log($(data).find("script#scriptID").contents());