dtmsaqtly798322992 2015-06-10 04:25
浏览 8
已采纳

简单的jQuery + PHP无法正常工作

I have the following code in my javascript:

$.ajax({
    url : 'update_data.php',
    type : 'POST',
    data : 'asdfasd', //d,
    success : function(response){
        console.log(response);
    },
    error: function(jqXHR, textStatus, errorThrown){
        console.log('jqXHR.responseText:  ' + jqXHR.responseText);
        console.log('jqXHR.responseXML :  ' + jqXHR.responseXML);
        console.log('textStatus:   ' +  textStatus);
        console.log('errorThrown:   ' + errorThrown);
    },
    dataType : 'text'
});

Here is my 'update_data.php' :

<?php
    echo json_encode($_POST);
    if (isset($_POST['data'])){
        echo "here!";
    } else {
        echo "failed jquery";
    }
?>

When I run the Ajax method, I get the following response in my console:

[]failed jquery

meaning update_data.php didn't get any POST request. '[]' is from json_econd($_POST), and 'failed jquery' is from the if/else.

What am I doing wrong?

  • 写回答

4条回答 默认 最新

  • duanjiao1256 2015-06-10 04:26
    关注

    you need to pass data as below

     data : {'data':'asdfasd'},
    

    if want to pass multiple parameters then

    data : {'data':'asdfasd','param1':'value','param2':'value'},
    

    or submit data of form

    data : $( "formselector" ).serialize(),
    

    data parameter in ajax

    Type: PlainObject or String or Array Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

    Read more about jquery ajax parameters HERE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?