Json 角度:HttpErrorResponse:“;对…“进行分析时Http失败”从服务器成功返回的字符串
springboot-RESTful服务器端; 将返回字符串的测试方法:Json 角度:HttpErrorResponse:“;对…“进行分析时Http失败”从服务器成功返回的字符串,json,string,angular,parsing,string-parsing,Json,String,Angular,Parsing,String Parsing,springboot-RESTful服务器端; 将返回字符串的测试方法: @RequestMapping(value = "test", method = RequestMethod.GET) public ResponseEntity<String> test(HttpServletRequest req, HttpServletResponse resp) { try { return new ResponseEntity<S
@RequestMapping(value = "test", method = RequestMethod.GET)
public ResponseEntity<String> test(HttpServletRequest req, HttpServletResponse resp) {
try {
return new ResponseEntity<String>("Test has worked, biatch!", HttpStatus.OK);
} catch (Exception e) {
System.err.println("## EXCEPTION: " + e.getMessage());
return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
有趣的是,它包含从服务器返回的字符串,我可以通过subscribe函数上的error.text
访问它。
控制台上的错误对象:
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8080/theater/admin/test", ok: false, …}
error
:
{error: SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttp…, text: "Test has worked, biatch!"}
headers
:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message
:
"Http failure during parsing for http://localhost:8080/theater/admin/test"
name
:
"HttpErrorResponse"
ok
:
false
status
:
200
statusText
:
"OK"
url
:
"http://localhost:8080/theater/admin/test"
__proto__
:
HttpResponseBase
HttpErrorResponse{headers:HttpHeaders,状态:200,状态文本:“OK”,url:http://localhost:8080/theater/admin/test“,ok:false,…}
错误
:
{错误:SyntaxError:在XMLHttp的JSON.parse()位置0处的JSON中出现意外的标记T…,文本:“测试已成功,biatch!”}
标题
:
HttpHeaders{normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ}
消息
:
“在分析的过程中Http失败http://localhost:8080/theater/admin/test"
名称
:
“HttpErrorResponse”
好啊
:
假的
地位
:
200
状态文本
:
“好的”
网址
:
"http://localhost:8080/theater/admin/test"
__原型__
:
HttpResponseBase
这可能与解析从服务器返回的包含字符串的JSON对象有关。
但是,返回对象、集合和其他任何对象都可以正常工作。subscribe()正确解析我从服务器获得的任何对象,或者如果服务器发生异常,则返回的HttpStatus会正确调用客户端的HttpErrorResponse
那么,像这样的字符串错误发射是怎么回事?不管发生什么,我总是收到HttpErrorResponse。我做错什么了吗
测试成功了,biatch
这不是JSON。因此分析错误
这可能与解析从返回的JSON对象有关
服务器,包含字符串。但是返回的对象,,
集合和其他任何东西-工作完全正常-.subscribe()
它对POJOs有效,因为它们是JSON编码的。这里有普通的字符串
要将响应作为字符串而不是对象获取,请执行以下操作
http.get(url, {responseType: 'text'})
试试这样的东西-
{ responseType: 'text' as 'json' }
我在Angular 7中遇到了与HttpClient相同的问题,使用上述设置解决了这个问题
问题在于,当您在get
或post
上使用返回类型时,或者像这样从httpClient使用任何方法时-
this.http.get<FooClass>(url)
this.http.get(url)
它假定响应类型是JSON,因此它期望响应中有一个JSON对象。将响应类型设置为“text”为“json”将强制执行文本响应。我通过使用
JsonObject
正确构建json字符串,然后将该字符串插入响应属性中,解决了这个问题:
@PostMapping(...)
public ResponseEntity handler(HttpServletRequest req, HttpServletResponse resp) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("text", "Hello World!");
JsonObject json = builder.build();
return new ResponseEntity<>(json.toString(), HttpStatus.OK);
}
@PostMapping(…)
公共响应属性处理程序(HttpServletRequest-req、HttpServletResponse-resp){
JsonObjectBuilder=Json.createObjectBuilder();
添加(“文本”,“你好,世界!”);
JsonObject json=builder.build();
返回新的ResponseEntity(json.toString(),HttpStatus.OK);
}
JSON格式不正确。可能根本不是JSON那么,我应该如何在客户端以不同的方式处理这个问题呢?那么,我应该如何在客户端以不同的方式处理这个问题呢?尝试http.get(url,{responseType:text'}
在阅读了这个答案后,我在文档中也找到了它:我尝试了许多方法,这是唯一有效的方法。
@PostMapping(...)
public ResponseEntity handler(HttpServletRequest req, HttpServletResponse resp) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("text", "Hello World!");
JsonObject json = builder.build();
return new ResponseEntity<>(json.toString(), HttpStatus.OK);
}