简体   繁体   中英

PHP Ajax works with get but not with post

I am trying to send a some data via ajax post and on server perform some task and then return result back. here is an ex: i have done

  function sometask(id) {
    $.ajax({
        type: 'POST',
        url: '/ajaxPost/task/',
        data: {title: 'test', text: 'test2'},
        dataType: 'text',
        success: function(data) {
            console.log('success');
            console.log(data);
        },
        error: function () {
            console.log('failed');
        }
    });
  }

on server side

<?php
var_dump($_POST);
exit;

on html Do something

expected result was data sent via ajax returned as an array. but its returning empty array.

when changed type to 'GET' on ajax and on server side var_dump($_GET); the data is returned.

So why does it work with get and not with post, where i have gone wrong?

in google chrome F12 networktab..

see how data is send, and compare with a normal post request (without jquery ajax)

you probably get the answer

I think it just depends on how complex of a task you are trying to run. The $.ajax function is for tasks that require better error reporting as well as other callbacks that you can access to. I usually run just a $.post function to complete most data exchanges. Not sure if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM