小编Amy*_*Amy的帖子

HTTPClient POST尝试解析非JSON响应

我正在尝试在Angular中发出请求,我知道HTTP响应不是JSON而是文本.但是,Angular似乎期待JSON响应,因为错误如下:

SyntaxError:XMLHttpRequest.c中JSON.parse()位置0的JSON中的意外标记<

以及

解析http:// localhost:9时出现 Http失败...

这是post方法:

return this.http.post(this.loginUrl, this.createLoginFormData(username, password), this.httpOptions)
  .pipe(
    tap( // Log the result or error
      data => console.log(data);
      error => console.log(error)
    )
  );
Run Code Online (Sandbox Code Playgroud)

和标题.

private httpOptions = {

  headers: new HttpHeaders({
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Content-Type': 'application/x-www-form-urlencoded',
    responseType: 'text'
  },

) };
Run Code Online (Sandbox Code Playgroud)

我认为这responseType: 'text'足以让Angular期望非JSON响应.

typescript angular angular-httpclient

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

使用strtok解析mmaped文件?

这是我的问题:我想映射文件"filename.txt",它基本上由每行两对字符串组成:

"string1 string2
 string3 string4
 string5 string6..."
Run Code Online (Sandbox Code Playgroud)

然后我想用strtok分隔不同的字符串.

所以我像这样映射文件:

// open file
if ((fdsrc = open("filename.txt", O_RDONLY)) < 0) {
        fprintf(stderr, "src open error");
        exit(1);
    }

// get the size of the file
if (fstat(fdsrc, &statbuf) < 0) {
    fprintf(stderr, "fstat error");
    exit(1);
}

// mmap the file
if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdsrc, 0)) == (caddr_t) -1) {
    fprintf(stderr, "mmap src");
    exit(1);
}
Run Code Online (Sandbox Code Playgroud)

当我跑线

printf("src: %s \n", src);
Run Code Online (Sandbox Code Playgroud)

它正确打印文件的内容!

但是当我试图分开这些词时

char* token;
token = strtok(src, " \n"); …
Run Code Online (Sandbox Code Playgroud)

c string parsing mmap strtok

4
推荐指数
1
解决办法
600
查看次数

标签 统计

angular ×1

angular-httpclient ×1

c ×1

mmap ×1

parsing ×1

string ×1

strtok ×1

typescript ×1