How can I post data as form data instead of a request payload?

前端 未结 22 2251
庸人自扰
庸人自扰 2020-11-22 00:13

In the code below, the AngularJS $http method calls the URL, and submits the xsrf object as a \"Request Payload\" (as described in the Chrome debugger network t

22条回答
  •  一整个雨季
    2020-11-22 00:41

    As a workaround you can simply make the code receiving the POST respond to application/json data. For PHP I added the code below, allowing me to POST to it in either form-encoded or JSON.

    //handles JSON posted arguments and stuffs them into $_POST
    //angular's $http makes JSON posts (not normal "form encoded")
    $content_type_args = explode(';', $_SERVER['CONTENT_TYPE']); //parse content_type string
    if ($content_type_args[0] == 'application/json')
      $_POST = json_decode(file_get_contents('php://input'),true);
    
    //now continue to reference $_POST vars as usual
    

提交回复
热议问题