我尝试将Angular 4的POST请求发送到我的Laravel后端.
我的LoginService有这个方法:
login(email: string, password: string) {
return this.http.post(`http://10.0.1.19/login`, { email, password })
}
Run Code Online (Sandbox Code Playgroud)
我在LoginComponent中订阅了这个方法:
.subscribe(
(response: any) => {
console.log(response)
location.reload()
},
(error: any) => {
console.log(error)
})
Run Code Online (Sandbox Code Playgroud)
这是我的Laravel后端方法:
...
if($this->auth->attempt(['email' => $email, 'password' => $password], true)) {
return response('Success', 200);
}
return response('Unauthorized', 401);
Run Code Online (Sandbox Code Playgroud)
我的Chrome开发工具说我的请求是成功的200状态代码.但我的Angular代码触发了error阻止并给了我这条消息:
解析http://10.0.1.19/api/login期间的Http失败
如果我从后端返回一个空数组,它可以工作......所以Angular试图将我的响应解析为JSON?我怎么能禁用它?
我想使用这个 Angular 6 代码实现文件下载:
休息API:
private static final Logger LOG = LoggerFactory.getLogger(DownloadsController.class);
@GetMapping(path="export")
public ResponseEntity<byte[]> export() throws IOException {
File pdfFile = Paths.get(EXTERNAL_FILE_PATH).toFile();
byte[] fileContent = Files.readAllBytes(pdfFile.toPath());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
// Here you have to set the actual filename of your pdf
String filename = "output.pdf";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
return response;
}
Run Code Online (Sandbox Code Playgroud)
服务:
import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from "@angular/common/http";
import {Observable} from "rxjs/index";
import {environment} …Run Code Online (Sandbox Code Playgroud)