AJAX未加载JSON对象


                    

我正在尝试使用首字母缩写finder API进行简单的GET请求,但由于某种原因,未返回JSON。 这是代码:</ p>

  $ .ajax('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD',{
     crossDomain:true,
     dataType:“ jsonp”,
     成功:功能(数据){
       console.log(data);
     }
});
</ code> </ pre>

如果我直接在浏览器中访问该网址,则可以轻松看到请求的JSON,但我的Chrome控制台仅返回:</ p>

 资源被解释为脚本,但以MIME类型的text / plain传输:“ http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD&callback=jQuery1111025898682116530836_1417074190743&_=1417074190744”。
</ code> </ pre>

Chrome调试器的网络标签显示下载的文件正确,但是为什么不将JSON记录到控制台中?</ p>
     </ div>

展开原文

原文

I'm trying to do a simple GET request with an acronym finder API but for some reason the JSON is not being returned. Here is the code:

   $.ajax('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD', {
    crossDomain:true, 
    dataType: "jsonp", 
    success: function(data){
      console.log(data);
    }
});

If I visit the url directly in my browser, I can easily see the requested JSON, but my Chrome console only returns:

Resource interpreted as Script but transferred with MIME type text/plain: "http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD&callback=jQuery1111025898682116530836_1417074190743&_=1417074190744".

The Chrome Debugger network tab indicates that the correct file was downloaded but why isn't the JSON being logged to the console?

2个回答


错误消息指示响应MIME类型为“文本/纯文本”。 但它期望使用脚本MIME类型。 </ p>

“在此处输入图片描述” </ p>

因此,您需要在后端 dictionary.py </ code>上设置响应(如果它在您的控制之下)。 将内容类型“ application / x-javascript” </ code>添加到响应标头。 类似于(在Java中):</ p>

  @RequestMapping(value =“ / test / fake”)
公共无效testFake(HttpServletRequest请求,
         HttpServletResponse响应)引发IOException {
     字符串回调= request.getParameter(“ jsonpcallback”);
     SimpleJson json =新的SimpleJson(“ Success”,0);
     JSONObject resultJSON = JSONObject.fromObject(json);
     response.setContentType(“ application / x-javascript”); //将内容类型设置为“ application / x-javascript”
     PrintWriter out = response.getWriter();
     if(callback == null){
         out.println(“ {错误:未定义回调函数。'}”);
     }其他
         out.println(callback +“(” + resultJSON.toString(1,1)+“)”);
}
</ code> </ pre>
     </ div>

展开原文

原文

The error message indicates that the response MIME type is 'text/plain'. But it is expecting a Script MIME type.

enter image description here

So you need to setup your response at backend dictionary.py (if it is under your control). Add content-type "application/x-javascript" to the response header. Something similar to (in Java):

@RequestMapping(value = "/test/fake")
public void testFake(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    String callback = request.getParameter("jsonpcallback");
    SimpleJson json = new SimpleJson("Success", 0);
    JSONObject resultJSON = JSONObject.fromObject(json);
    response.setContentType("application/x-javascript");   // Set content-type to "application/x-javascript"
    PrintWriter out = response.getWriter();
    if (callback == null) {
        out.println("{error: 'Callback function is not defined.'}");
    } else
        out.println(callback + "(" + resultJSON.toString(1, 1) + ")");
}


尝试使用此</ p>

  $ .ajax('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD',{
   crossDomain:true,
   dataType:“ json”,
   contentType:“ application / json”,
   成功:功能(数据){
     console.log(data);
   }
});
</ code> </ pre>
     </ div>

展开原文

原文

Try using this

$.ajax('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD', {
  crossDomain:true, 
  dataType: "json", 
  contentType: "application/json",
  success: function(data){
    console.log(data);
  }
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问