【问题标题】:How to change Response Header Content-Type from txt/html, Ubuntu, tomcat8如何从 txt/html、Ubuntu、tomcat8 更改响应标头内容类型
【发布时间】:2017-02-11 23:11:21
【问题描述】:

我正在调用 API 以返回带有所有需要的对象(注释)的 JSON

$( document ).ready(function() {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
      var response = xhr.responseText;
               //do stuff with response        
    }
    xhr.open('GET', 'http://mydomain:8080/notes/all', true);
    xhr.send(null);
  });

HTTP 请求:

@RequestMapping(value = {"/all"}, method = RequestMethod.GET, produces = "application/json")
public  @ResponseBody List<Note> getAllNotes() {
    return noteService.getAllNotes();
}

当我在 Mac 上本地运行时,一切正常。我的 Response 标头有 Content-Type: application/java,并且 Request 接受它。

但是,当我将 .war 上传到 Ubuntu 16.04 服务器时,请求标头的 Content-Type 更改为 txt/html,然后我得到了

404 页面未找到

。 我在两台机器上都使用tomcat8.5.5

如何指定/更改请求标头内容类型?

标题图片: 远程运行 和本地

【问题讨论】:

  • 内容类型是 text/html 因为返回了错误页面 404,它是一个 html 页面并且是有效的内容类型。您需要解决的是为什么在 Ubuntu 上返回 404。参考您的 Javascript 代码,该代码是否来自同一个应用程序?

标签: javascript spring ubuntu tomcat server


【解决方案1】:

我注意到我在 index.html 和 notes.html 中都有 &lt;script src="noteAPI.js"&gt;&lt;/script&gt; 声明。我已经从 index.html 中删除了这个声明并且它起作用了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 2013-10-01
    • 2020-11-20
    • 2021-08-22
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2021-05-23
    相关资源
    最近更新 更多