无法对'XMLHttpRequest'执行'setRequestHeader':值不是有效的ByteString

时间:2019-09-11 11:20:50

标签: angular ionic-framework ionic4

当我尝试在我的角度应用程序中上传base64图像时,http.post方法会引发错误:

Failed to execute 'setRequestHeader' on 'XMLHttpRequest': Value is not a valid ByteString.

这是代码的样子:

let headers = new Headers();
      headers.append("Accept", 'application/json');
      headers.append("Content-Type", "application/json");
      headers.append('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/68.0' );

      let postData = {
        "AudometerCapture":this.abc,                          
        "Door1":this.abc,
        "Door2":this.abc,
       "Door3":this.abc,
        "Door4":this.abc,
       "TransactionID": 90
    }

      this.http.post('http://apiearningwheels.sharpnettechnology.com/api/DailyImageUpload/UploadDailyImages', JSON.stringify(postData), {headers: headers})
        .map(res => res.json())
        .subscribe(data => {
          console.log(data);
          this.showLongToast("result is :- " + res);
        });

我希望输出为'result is = ',但出现错误。

使用Postman,请求可以正常工作。

1 个答案:

答案 0 :(得分:1)

抛出该错误是因为最后一个标头中有三个点(...)的符号,这些符号不能正确转换为ByteString。

因此,您必须删除行或进行如下更改:

headers.append('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/68.0' );

headers.append('User-Agent', 'Mozilla/5.0 (Windows NT 10.0;) Gecko/20100101 Firefox/68.0' );

我使用https://codesandbox.io/s/angular测试了标头,这使我在该行上犯了一个错误:Cannot convert string to ByteString because the character at index 30 has value 8230 which is greater than 255.