React Native 无法读取API

py49o6xq  于 2023-02-13  发布在  React
关注(0)|答案(1)|浏览(160)

我可以在浏览器中读取此URL,但不能使用javascript
https://www.planete-sorties.net/api/api/ressources/types
此服务生成如下:

@RequestMapping(value = URL_RESOURCES_TYPES, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getTypes() {
    String serviceResult = paramService.getTypes().toString();
    return ResponseEntity.ok().body(serviceResult);
    
}

实际上,我正在构建一个react原生应用,错误是

[类型错误:网络请求失败]

我也试着在这里读取网址https://reqbin.com/,有一个错误。
在Java中我可以这样读

UriComponentsBuilder uriBuilder = 
UriComponentsBuilder.fromHttpUrl(appendHost(URL_RESOURCES_TYPES));
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(null, getHeaders());
RestTemplate restTemplate = restTemplateConfig.restTemplate();
ResponseEntity<String> serviceResultRE = restTemplate.exchange(uriBuilder.toUriString(), HttpMethod.GET, requestEntity, String.class);
7cjasjjr

7cjasjjr1#

代替

public ResponseEntity<String> getTypes() {

用途

public ResponseEntity<Map<String, Object>> getTypes() {
   return ResponseEntity.ok().body(new JSONObject(string).toMap());
}

相关问题