Angular HTTP发布不接受JSON响应? [英] Angular HTTP post not accepting JSON response?

查看:91
本文介绍了Angular HTTP发布不接受JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel中创建了一个API,并在邮递员中进行了测试,它运行良好.但是,当我从角度尝试时,它可以很好地返回字符串文本,但不能返回JSON响应

I created an API in laravel and tested in postman and it is working perfect. But when I try it from angular it works fine for returning a string text but not for JSON response

我在Internet上搜索了它,发现设置了content-type:application/json,并尝试了以各种方式在标头中设置内容类型,但是问题仍然存在

I searched it on internet and found setting content-type:application/json and tried with different ways for setting content type in header but issue still persist

    var obj = JSON.parse('{"email":"ab@gm.com","password":"12345678"}');
    //1st type of header
    var headers_object = new HttpHeaders().set('Content-Type', 
    'application/json');

     const httpOptions = {
         headers: headers_object
     };

    //2nd type of header
        var HTTPOptions = {
            headers: new HttpHeaders({
               'Accept':'application/json',
               'Content-Type': 'application/json'
            }),
            'responseType': 'application/json' as 'json'
         }

        return this.http.post<any>(`http://127.0.0.1:8000/api/auth/login`, obj,HTTPOptions ).subscribe(resp => {
            console.log(resp);    
        });

邮递员输出

浏览器网络请求/响应

browser network request/response

推荐答案

这是由于CORB引起的.

It was due to CORB.

跨源读取阻止(CORB)是一种算法,可以识别并在之前阻止Web浏览器中可疑的跨域资源加载他们到达网页.CORB降低了敏感液体泄漏的风险通过使其与跨源网页保持更远的距离来获取数据.

Cross-Origin Read Blocking (CORB) is an algorithm that can identify and block dubious cross-origin resource loads in web browsers before they reach the web page. CORB reduces the risk of leaking sensitive data by keeping it further from cross-origin web pages.

https://www.chromestatus.com/feature/5629709824032768

解决方案在禁用的网络安全模式下运行chrome.这对我有用 https://stackoverflow.com/a/42086521/6687186

Solution run chrome in disabled web security mode. This worked for me https://stackoverflow.com/a/42086521/6687186

Win + R并粘贴

Win+R and paste

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security 

这篇关于Angular HTTP发布不接受JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆