简体   繁体   English

Angular 2 Post:不向服务器发送“内容类型:应用程序/json”

[英]Angular 2 Post: Not sending 'content-type: application/json' to server

I'm really struggling with POST-requests in Angular 2. I'm able to send a request with certain parameters, but my backend (PHP Slim v3) can't get the parameters.我真的很努力处理 Angular 2 中的 POST 请求。我可以发送带有某些参数的请求,但是我的后端 (PHP Slim v3) 无法获取这些参数。 Therefore I investigated my request, and realized that my Angular request sends 'content-type: application/text-plain'.因此,我调查了我的请求,并意识到我的 Angular 请求发送了“内容类型:应用程序/文本纯文本”。 Therefore my backend doesn't have access to the variables.因此我的后端无法访问这些变量。

Then, I read many tutorials, browsed here on stack overflow and came to the conclusion that I have to append the header.然后,我阅读了很多教程,浏览了堆栈溢出,得出的结论是我必须附加标题。

My angular class looks like this:我的角度类看起来像这样:

/**
* Generic method for all POST-requests.
* 
* @author 
* @date 08.01.2017
*/
postData(apiUrl, data): any
{

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    let body = JSON.stringify(data)

    return this.http.post(apiUrl, body, options)
        .map((responseData) => 
        {
            if(responseData.status === 200)
            {
                return this.extractData(responseData);
            }
            else
            {
                this.handleRestError(null); 
            }
        })
        .catch(res => {
            return this.handleRestError(res);
       });
}

So all in all pretty simple.所以总的来说很简单。 However, when I send that request, the strange thing is, that it somehow recognizes that as an OPTIONS request and gives me 'Unsupported method: OPTIONS'.但是,当我发送该请求时,奇怪的是,它以某种方式将其识别为 OPTIONS 请求并给我“不支持的方法:OPTIONS”。

Please see the request header here:请在此处查看请求标头:

OPTIONS /auth/new HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

My response from the backend looks the following:我的后端回复如下:

HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.6.28
Set-Cookie: PHPSESSID=aa546thl9maj7hamjj64vpbe95; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-type: text/plain;charset=UTF-8
Allow: POST
Access-Control-Allow-Methods: OPTIONS
Content-Length: 21

And the response from the server looks like this:来自服务器的响应如下所示:

Allowed methods: POST

However, I have managed to get a proper response from the server with omitting the options parameter in the post request.但是,我设法通过省略 post 请求中的options参数从服务器获得了正确的响应。 The request then gets sent properly and I see the desired parameters in the request payload in Chrome console.然后请求被正确发送,我在 Chrome 控制台的请求负载中看到了所需的参数。 The problem is that I can't get access to the variables on backend, because it keeps giving me a 'content-type: text/plain'.问题是我无法访问后端的变量,因为它一直给我一个“内容类型:文本/纯文本”。

Any idea what I'm doing wrong?知道我做错了什么吗?

Thanks for help in advance!提前感谢您的帮助!

this because you face with CORS problem, so you need allow with OPTIONS method too (backend).这是因为您面临 CORS 问题,因此您也需要允许 OPTIONS 方法(后端)。 something like this:像这样:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 86400

Learn more about CORS here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS在此处了解有关 CORS 的更多信息: https : //developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular 发布请求,内容类型应用程序/json 不起作用 - angular post request with content-type application/json not working 无法设置content-type = application / json angular4发布请求 - Unable to set content-type = application/json angular4 post request Angular 2 - POST 未将 Content-Type 设置为 application/json(其文本/纯文本) - Angular 2 - POST does not set Content-Type to application/json (its text/plain) Angular 2,Http和Not设置Content-Type“application / json” - Angular 2, Http and Not setting Content-Type “application/json” 带有 'Content-Type': 'application/x-www-form-urlencoded' 和 responseType: 'text' 的有角度的 http 帖子 - angular http post with 'Content-Type': 'application/x-www-form-urlencoded' and responseType: 'text' Angular2(前)和Yii2(结束)-具有选项'Content-Type'的http.get:'application / json' - Angular2 (front) and Yii2 (end) - http.get with option 'Content-Type': 'application/json' angular v5.1.0 HttpClient未设置头内容类型:application / json - angular v5.1.0 HttpClient not set header content-type : application/json Angular 5 'Content-Type':'multipart/form-data' 被重置为 'application/json' - Angular 5 'Content-Type': 'multipart/form-data' gets reset to 'application/json' Angular 8 HttpClient 发布请求内容类型崩溃 - Angular 8 HttpClient Post Request Content-Type debacle 带有“内容类型”的 Angular 2 标头:“text/xml”,http 帖子不起作用 - Angular 2 headers with 'content-type' : 'text/xml', http post not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM