后端php上$ _POST未看到角Http帖子请求

问题描述 投票:0回答:2

我正在为WordPress网站构建PHP自定义终结点,以在woocommerce产品页面上提交表单,add to cart表单要具体。

所以我有一个基于ionic 4 / angular 8的移动应用程序,需要将发布请求发送到上述端点。

问题是post通过$ _ POST$ ange>加入购物车!但是在使用邮递员进行测试时,请求成功并将产品成功添加到购物车。 我试图在PHP端获取有角的JSON主体并成功并获得成功响应,但产品未添加到购物车中,这意味着表单需要一种类似于邮递员请求的特定发送方式。

PHP代码非常简单:

function yith_custom_add_item( WP_REST_Request $request ) { $currentuserid_fromjwt = get_current_user_id(); $user = get_user_by( 'id', $currentuserid_fromjwt ); if( $user ) { $product_id = $_GET['id']; $url = get_permalink( $product_id ); $cookie = 'wordpress_logged_in_856ec7e7dd32c9b2fc11b366505cf40d' . '=' . wp_generate_auth_cookie($currentuserid_fromjwt, time() + (10 * 365 * 24 * 60 * 60), 'logged_in') . ';'; $cookie .='_icl_current_admin_language_d41d8cd98f00b204e9800998ecf8427e=en; wp_woocommerce_session_856ec7e7dd32c9b2fc11b366505cf40d=171%7C%7C1575326201%7C%7C1575322601%7C%7Cab91a0f0bbe0f3d5ae90dd5bdf7e396d; wfwaf-authcookie-5372c020d00bf5e1ea6c81123ff21ec1=171%7C%7C4574b4a9b1b62c8c7a1c1ae24cd4bd26d279bb5670fe87bcf7eb210835e43f22; _icl_current_language=en; PHPSESSID=5ed2009813f86633694a25e297d4ee06; wordpress_logged_in_856ec7e7dd32c9b2fc11b366505cf40d=deleted; woocommerce_items_in_cart=1; woocommerce_cart_hash=086ae7e00c53740163451a538fe8a9fc'; $angular_json = json_decode(file_get_contents("php://input")); if ( !empty($_POST) ) { $response = wp_remote_post( $url, array( 'headers' => array( 'Cookie' => $cookie, 'Authorization' => $request->get_header('Authorization'), 'Content-Type' => 'application/x-www-form-urlencoded', ), 'body' => $_POST, )); return $response; } elseif ( !empty($angular_json) ) { $response = wp_remote_post( $url, array( 'headers' => array( 'Cookie' => $cookie, 'Authorization' => $request->get_header('Authorization'), 'Content-Type' => 'application/x-www-form-urlencoded', ), 'body' => $angular_json, )); return $angular_json; } else { return "Nothing to show"; } } }

Angular代码:
(服务)

addYithCompositeToCart(id: any, data: any) { const { token } = JSON.parse(localStorage.getItem('user')); let headers = { 'Content-Type': 'x-www-form-urlencoded; charset=UTF-8', "Authorization": `Bearer ${token}`, }; // let headers = new HttpHeaders(); // headers // .set('Content-type', 'x-www-form-urlencoded;') // .set('Authorization', `Bearer ${token}`); this.http.post(`${environment.host}yith-composite/add-item?id=${id}`, data, { headers }) .toPromise() .then((data) => { console.log(data); }) .catch((error) => { console.log(error); }); }

组件(通过单击触发):

addToCart() { const data = { ywcp_selection: Object.assign({}, ...this.options.map(object => ({ [object.productSerial]: -1 }))), ywcp_variation_id: Object.assign({}, ...this.options.map(object => ({ [object.productSerial]: object.variation.variation_id }))), ywcp_quantity: Object.assign({}, ...this.options.map(object => ({ [object.productSerial]: 1 }))), ywcp_selected_product_value: Object.assign({}, ...this.options.map(object => ({ [object.productSerial]: object.productId }))), quantity: 1, 'add-to-cart': this.product.id }; this.cartService.addYithCompositeToCart(this.product.id, data); }

邮递员要求
enter image description here

我正在为WordPress网站构建PHP自定义终结点,以在woocommerce产品页面上提交表单,并具体添加到购物车表单。所以我有一个基于ionic 4 / angular 8的移动应用程序,需要...

尝试使用Observable和Susbscribe修改代码,

service.ts

return this.http .post(`${environment.host}yith-composite/add-item?id=${id}`,JSON.stringify(data),{ headers: headers })

然后在组件中,
this.cartService.addYithCompositeToCart(this.product.id, data).subscribe((data)=>{
     console.log(data);
});

您是否尝试在HttpOptions中指定内容类型,并且它是您在正文中提供的json数据?
对于example

import { HttpHeaders } from '@angular/common/http'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization": `Bearer ${token}`, }) };

javascript php angular wordpress
2个回答
0
投票

0
投票
对于example
© www.soinside.com 2019 - 2024. All rights reserved.