0

VUEjs code:

  const API_URL = "http://localhost:8880/php/";
  axios({
    method: "post",
    url: API_URL + "test.php",
    data: {
      optn: "procdata",
      mdate1: this.mdate1,
      mdate2: this.mdate2,
      mmcc: "",
    },
  }).then((respone) => {
    console.log(respone);
  });

PHP Code:

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
header('Access-Control-Allow-Headers: token, Content-Type');

echo $_POST['optn'];
echo $_POST['mdate1'];
echo $_POST['mdate2'];
echo $_POST['mmcc'];

i am getting this error in browser console as response:


Notice: Undefined index: optn in F:\xampp\htdocs\php\test.php on line 7

Notice: Undefined index: mdate1 in F:\xampp\htdocs\php\test.php on line 8
  • https://stackoverflow.com/a/4261200/128761 – vee Nov 13 '21 at 12:09
  • @vee thanks for the link. But it's just showing how to remove the warning. But i want to know why my post method is not working? – Kvr Technologies Nov 13 '21 at 12:22
  • Clearly from those notice messages your POST values are non-existent when you get to your PHP code. **Suggestion:** the URL within the ajax methods should always be absolute path (in your case /php/test.php). [Also check this link out and try performing the post request the same way in the example.](https://axios-http.com/docs/post_example) – slashroot Nov 13 '21 at 12:25
  • Undefined index nnn means array key `nnn` is not set. In your case it means there is no `$_POST['optn']` and `mdate`. So, check your JS code (Vue) to make sure that you have set these values. Try to debug with `var_dump($_POST)` to see what POST data sent to your server. – vee Nov 13 '21 at 12:25
  • do you have errors for mdate2 and mmcc too ? – Ken Lee Nov 13 '21 at 12:30
  • @KenLee Yes i have errors for that too. – Kvr Technologies Nov 13 '21 at 12:32
  • @KenLee i have tried the same code with get method and it worked fine but post is giving this warning response. – Kvr Technologies Nov 13 '21 at 12:33
  • @vee yes i have tried with var_dump($_POST) is shows array(0) – Kvr Technologies Nov 13 '21 at 12:35
  • I've never use Vue before. From [axios](https://github.com/axios/axios) document you can still call URL with method POST using `.post()`. Did you try that? – vee Nov 13 '21 at 12:37
  • @vee i have tried but still did'nt understand why `.get()` method is working with same url and prameters but `.post()` is not working. – Kvr Technologies Nov 13 '21 at 12:44
  • Why are you using header() calls before echoing $_POST? – lukas.j Nov 13 '21 at 12:48
  • @lukas.j because it shows me this warning CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource – Kvr Technologies Nov 13 '21 at 12:49
  • Does this answer your question? [Axios POST request not working](https://stackoverflow.com/questions/51379356/axios-post-request-not-working) – Ken Lee Nov 13 '21 at 12:49
  • As I said, I never use these things before so I may need more and more test and result from you. Sorry for that but... Can you please use your web browser **network inspector** to see that what data has sent to the server? This is for make sure that the JS already sent the data in method `POST`. – vee Nov 13 '21 at 12:50
  • @Kvr Technologies: you should put that info into your original post. That is an important part of your question. – lukas.j Nov 13 '21 at 12:52
  • @KenLee No but now i found the issue.......... the issue was at php side i have to write if($_SERVER["REQUEST_METHOD"]=="POST" && empty($_POST)) { $_POST = json_decode(file_get_contents('php://input'),true); } in php to extract the $_POST values. – Kvr Technologies Nov 13 '21 at 12:53

1 Answers1

0

i have identified the issue in

PHP Code:

if($_SERVER["REQUEST_METHOD"]=="POST" && empty($_POST))
{
    //CONVERT THE POST INPUT REQUEST TO POST ARRAY
    $_POST = json_decode(file_get_contents('php://input'),true);
}

we need to write the above code in php before using $_POST

  • I believe what you have done is parsing the request body as JSON. It was necessary because, by default, Axios sends your payload encoded as `application/json`. An alternative would be keeping `$_POST` as it is and changing your request at your JavaScript code as `application/x-www-form-urlencoded`, which PHP supports out-of-the-box. See https://axios-http.com/docs/urlencoded. – Tasso Evangelista Nov 13 '21 at 15:20