2017-03-06 43 views
0

我試圖開發一個小應用程序來在本地elasticsearch引擎上創建索引。我在前端使用angularjs,在後端使用spring-boot。它可以成功創建索引,但是,當我想要回顧前端的響應時,它會一直拋出錯誤。Angularjs http沒有來自Spring Boot的響應

下面是我AngularJS API調用:

app.service('ESIndexService', function($http) { 

    this.createESIndex = function(esindexObj) { 
     var settings = { 
       method: 'POST', 
       url: BASE_URL + "/ESIndex/createESIndex", 
       data: esindexObj 
      }; 
     return $http(settings).then(function(response) { 
      console.log("response:"+response); 
      return response; 
     }, function(error) { 
      console.log("error:"+error); 
      return error; 
     }); 
    }; 


}); 

然後,這是我的控制器:

@CrossOrigin 
@RestController 
@RequestMapping(value = "ESIndex") 
public class ESIndexController { 

    @RequestMapping(value = "createESIndex", method = RequestMethod.POST) 
    public @ResponseBody String createIndex(@RequestBody ESIndex esIndex) { 
     try { 
      Settings settings = Settings.builder() 
        .put("xpack.security.user", String.format("%s:%s", Constants.ES_UNAME, Constants.ES_PWD)).build(); 
      TransportClient client = new PreBuiltXPackTransportClient(settings) 
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Constants.ES_HOST), Constants.ES_PORT)); 
      CreateIndexResponse response = client.admin().indices().prepareCreate(esIndex.getName()).setSettings(Settings.builder() 
        .put("index.number_of_shards", esIndex.getNumberOfShards()) 
        .put("index.number_of_replicas", esIndex.getNumberOfReplicas())).get(); 
      client.close(); 
      if(response.isAcknowledged() && response.isShardsAcked()) 
       return Constants.SUCCESS; 
      else 
       return "Fail to create index!"; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return e.getMessage(); 
     } 
    } 
} 

我想在AngularJS響應響應狀態和數據。然而,它一直拋出我的錯誤:

error:SyntaxError: Unexpected token i in JSON at position 0 

我沒有使用JSON.parse函數,爲什麼它會給我這樣的錯誤?


添加的responseType後: '文字',仍拋出同樣的錯誤,nextwork enter image description here

原來我需要添加 「transformResponse:未定義」 鉻,但是,在我的另一項目,我從來沒有這樣做過。有什麼不同?

AngularJS:

this.newBlog = function(blogObj) { 
    var settings = { 
     method: 'POST', 
     url: baseUrl + "/blog/newBlog.do", 
     data: blogObj 
    } 
    return $http(settings).then(function(response) { 
     return response; 
    }, function(error) { 
     return error; 
    }); 
}; 

Java的控制器:

@RequestMapping(value="newBlog.do", method=RequestMethod.POST) 
public @ResponseBody String newBlog(@RequestBody Blog blog, HttpServletRequest request) { 

    User createdBy = (User) request.getSession().getAttribute("user"); 
    if(createdBy == null) 
     return NO_SESSION_MSG; 
    else { 
     createdBy.setPwd(null); 
     blog.setCreatedAt(TQBUtilities.getCurrentTime()); 
     blog.setLastUpdatedAt(TQBUtilities.getCurrentTime()); 
     blog.setCreatedBy(createdBy); 
     return blogService.newBlog(blog); 
    } 

} 
+0

那麼,該服務不會返回有效的JSON。打開瀏覽器的開發工具(F12/Cmd-Alt-I),進入網絡面板,看看你得到了什麼迴應。您應該刪除'function(error)'回調函數,因爲它會將錯誤轉換爲成功。 –

+0

在做了一些更多的尋找之後,你可以嘗試這裏找到這個解決方案:http://stackoverflow.com/a/27765419/2002257 – jheimbouch

+0

@jheimbouch transformResponse:undefined作品! – TommyQu

回答

0

角度會自動試圖解析服務器的響應爲JSON。嘗試添加這對您的設置對象

responseType: 'text' 

所以

var settings = { 
    method: 'POST', 
    url: BASE_URL + "/ESIndex/createESIndex", 
    data: esindexObj, 
    responseType: 'text' 
}; 
+0

仍然拋出同樣的錯誤,你可以看看更新的問題嗎? – TommyQu

0

@jheimbouch添加 「transformResponse:未定義」 到HTTP調用,如下面,做工精細:

var settings = { 
     method: 'POST', 
     url: BASE_URL + "/ESIndex/createESIndex", 
     data: esindexObj, 
     transformResponse: undefined 
}; 

然而,爲什麼它在angularjs 1.6.2中是必需的嗎?當我使用AngularJS 1.4.8時,我不需要添加transformResponse屬性。