简体   繁体   English

PHP Ajax可与get一起使用,但不适用于post

[英]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. 我试图通过ajax发布发送一些数据,并在服务器上执行一些任务,然后返回结果。 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 在HTML上做点什么

expected result was data sent via ajax returned as an array. 预期结果是通过ajax发送的数据以数组形式返回。 but its returning empty array. 但其返回的空数组。

when changed type to 'GET' on ajax and on server side var_dump($_GET); 当在ajax和服务器端将类型更改为'GET'时var_dump($ _ GET); the data is returned. 数据被返回。

So why does it work with get and not with post, where i have gone wrong? 那么,为什么我在出错的地方使用get而不是post呢?

in google chrome F12 networktab.. 在Google Chrome F12网络标签中。

see how data is send, and compare with a normal post request (without jquery ajax) 查看数据的发送方式,并与正常的发布请求进行比较(无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. $ .ajax函数用于需要更好的错误报告以及您可以访问的其他回调的任务。 I usually run just a $.post function to complete most data exchanges. 我通常只运行$ .post函数来完成大多数数据交换。 Not sure if this helps. 不确定是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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