【问题标题】:sending html in JSONP request在 JSONP 请求中发送 html
【发布时间】:2014-03-19 04:13:17
【问题描述】:

我正在尝试在这里https://s3.amazonaws.com/lawkickstas/lawkick_html.json 提出请求...

{"html":"<div id=\'nates_widget\'>
  <div id=\'question\'></div>
  <form>
    <input type=\'text\' />
  </form>
  <div id=\'result\'></div>
  <div id=\'hints\'></div>
</div>"}

有了这个

$.getJSON("https://s3.amazonaws.com/lawkickstas/lawkick_html.json?callback=?",   function(result){
 //response data are now in the result variable
 alert(result);
});

但我收到一个错误的意外令牌:
我想知道 A. 这是否是从外部源检索 html 的方法和 B. 这个回调是什么,我应该把它放在哪里?

【问题讨论】:

    标签: jquery html ajax json jsonp


    【解决方案1】:

    @natecraft,你也可以使用$.getJSON

    $.getJSON("https://s3.amazonaws.com/lawkickstas/lawkick_html.json?callback=?",function(result){
    });
    function myJsonMethod(str) { console.log(str); }
    

    【讨论】:

    • 出于好奇,您知道为什么需要该函数将对象包装在 .json 文件中吗?如果没有 jQuery,你将如何做到这一点?
    • 希望对您有所帮助,stackoverflow.com/questions/11812440/…
    【解决方案2】:

    这是经过多次尝试后奏效的方法。在 .json 文件中,我提出了一个请求,我将其更改为这个(将其包装在一个函数中)...

    myJsonMethod({"html": "<div id='nates_widget'><div id='question'></div><form><input type='text' /></form><div id='result'></div><div id='hints'></div></div>"})
    

    然后拨打电话...

    $.ajax({
      type : "GET",
      url :"https://s3.amazonaws.com/lawkickstas/lawkick_html.json?callback=?",
      dataType :"jsonp",
      jsonp: false,
      jsonpCallback: "myJsonMethod",
      success : function(data){
         console.log(data, "WORKED");},
      error : function(httpReq,status,exception){
        alert(status+" "+exception);
     }
    });
    

    当 json 文件中的对象没有被 myJsonMethod() 包围时它不起作用,并且当 jsonpCallback: "myJsonMethod" 行不包括时也不起作用。为什么?天知道。

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 2018-01-27
      • 2013-02-06
      • 2016-05-30
      • 1970-01-01
      • 2012-12-29
      • 2015-06-15
      • 2013-11-23
      相关资源
      最近更新 更多