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条)

报告相同问题?