相关疑难解决方法(0)

Angular HttpClient不发送标头

这是我的代码:

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)

http-headers angular angular-httpclient

144
推荐指数
7
解决办法
16万
查看次数

无法对“XMLHttpRequest”执行“setRequestHeader”:值不是有效的字节字符串

当我尝试在 Angular 应用程序中上传 Base64 图像时,http.post 方法会抛出错误:

\n\n
Failed to execute \'setRequestHeader\' on \'XMLHttpRequest\': Value is not a valid ByteString.\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码如下所示:

\n\n
let 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        });\n
Run Code Online (Sandbox Code Playgroud)\n\n

我期望输出是,\'result is = \'但我收到错误。

\n\n

该请求使用 …

ionic-framework angular ionic4

2
推荐指数
1
解决办法
2万
查看次数