这是我的代码:
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
Run Code Online (Sandbox Code Playgroud)
logIn(username: string, password: string) {
const url = 'http://server.com/index.php';
const body = JSON.stringify({username: username,
password: password});
const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json; charset=utf-8');
this.http.post(url, body, {headers: headers}).subscribe(
(data) => {
console.log(data);
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
console.log('Client-side error occured.');
} else {
console.log('Server-side error occured.');
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
在这里网络调试:
Request Method:POST
Status Code:200 OK
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:46
Content-Type:text/plain …Run Code Online (Sandbox Code Playgroud) 当我尝试在 Angular 应用程序中上传 Base64 图像时,http.post 方法会抛出错误:
\n\nFailed to execute \'setRequestHeader\' on \'XMLHttpRequest\': Value is not a valid ByteString.\nRun Code Online (Sandbox Code Playgroud)\n\n代码如下所示:
\n\nlet headers = new Headers();\n headers.append("Accept", \'application/json\');\n headers.append("Content-Type", "application/json");\n headers.append(\'User-Agent\', \'Mozilla/5.0 (Windows NT 10.0; \xe2\x80\xa6) Gecko/20100101 Firefox/68.0\' );\n\n let postData = {\n "AudometerCapture":this.abc, \n "Door1":this.abc,\n "Door2":this.abc,\n "Door3":this.abc,\n "Door4":this.abc,\n "TransactionID": 90\n }\n\n this.http.post(\'http://apiearningwheels.sharpnettechnology.com/api/DailyImageUpload/UploadDailyImages\', JSON.stringify(postData), {headers: headers})\n .map(res => res.json())\n .subscribe(data => {\n console.log(data);\n this.showLongToast("result is :- " + res);\n });\nRun Code Online (Sandbox Code Playgroud)\n\n我期望输出是,\'result is = \'但我收到错误。
该请求使用 …