$ _POST未检测到从ajax

时间:2019-03-07 04:28:06

标签: javascript php reactjs yii redux-api-middleware

使用与redux-api-middlewareaxios类似的jquery.ajax,我传递了一个formData,它是图像和其他表单值的混合体,如您在此图像上看到的:{{ 3}}

我遇到的问题是,通过POST请求成功调用API后,尽管发生了实际的POST请求,PHP $_POST对象为null。这是我的代码段:

import { CALL_API } from "redux-api-middleware";

export function createTestAnnouncement(data) {
  return (dispatch, getState) => {
    const { auth: { oauthToken, oauthTokenSecret } } = getState();

    const formData = new FormData();

    Object.entries(data).forEach(([key, value]) => {
      if (key === 'image') {
        formData.append(key, value);
      } else {
        formData.set(key, value);
      }
    });

    return dispatch({
      [CALL_API]: {
        endpoint: "/api/test-announcements",
        method: "POST",
        headers: {
          'xoauthtoken': oauthToken,
          'xoauthtokensecret': oauthTokenSecret,
        },
        body: formData,
        types: [CREATE_TEST_ANNOUNCEMENTS, CREATE_TEST_ANNOUNCEMENTS_SUCCESS, CREATE_TEST_ANNOUNCEMENTS_FAILURE]
      }
    })
  }
}

如何从$_POST对象获取值?我是否正确使用了FormData对象?

编辑:我的控制器就是这样,PS:我确定这是可行的,因为它适用于简单的application/json请求

use api\controllers\BaseController;
use model\Operations\TestAnnouncements\TestAnnouncementOperation;
use model\DB\TestAnnouncement;

class IndexController extends BaseController
    public function actionCreate()
    {
        var_dump($_POST);
        // Commented this out because the payload is not JSON
        // $request = \Yii::app()->request;

        // $op = new TestAnnouncementOperation();
        // $op->topic = $request->getJSON('topic');
        ...
    }
...

我的var_dump总是为NULL。使用邮递员并在正文上传递表单数据时,会在$ _POST上为我生成一个值。

2 个答案:

答案 0 :(得分:0)

您可以使用...来检查变量是否已设置。

if(!isset($_POST["ur_varible_name_from_html_form"]))
{

  echo "error";
}

答案 1 :(得分:0)

  

您可以在github上查看## here is input_fn def input_fn(data_dir, is_training, batch_size): def parse_csv(value): ## here some process to create feature_indices list, feature_values list and labels return {"index": feature_indices, "value": feature_values}, labels dataset = tf.data.Dataset.from_tensor_slices(your_filenames) ds = dataset.flat_map( lambda f: tf.data.TextLineDataset(f).map(parse_csv) ) ds = ds.padded_batch(batch_size, ds.output_shapes, padding_values=( { "index": tf.constant(-1, dtype=tf.int32), "value": tf.constant(0, dtype=tf.float32), }, tf.constant(False, dtype=tf.bool) )) return ds.repeat().prefetch(batch_size) ## create feature column def build_model_columns(): categorical_column = tf.feature_column.categorical_column_with_identity( key='index', num_buckets=your_feature_dim) sparse_columns = tf.feature_column.weighted_categorical_column( categorical_column=categorical_column, weight_feature_key='value') dense_columns = tf.feature_column.embedding_column(sparse_columns, your_embedding_dim) return [sparse_columns], [dense_columns] ## when created feature column, you can put them into estimator, eg. put dense_columns into DNN, and sparse_columns into linear model. ## for export savedmodel def raw_serving_input_fn(): feature_spec = {"index": tf.placeholder(shape=[None, None], dtype=tf.int32), "value": tf.placeholder(shape=[None, None], dtype=tf.float32)} return tf.estimator.export.build_raw_serving_input_receiver_fn(feature_spec) 回购,问题#125。他们已经解决并给出了示例[CALL_API]和[RSAA]以及数据。