Please help with jquery + ajax request.

Discussion in 'jQuery' started by sergey6116, Sep 13, 2011.

  1. #1
    I'm trying to send an array with ajax request to php. Can't understand what I am doing wrong. Can someone please help? This is the code:

    <script>
    $(document).ready(function(){

    var array = $.makeArray(2);

    var url = window.location.pathname;
    var filename = url.match(/.*\/(.*)$/)[1];

    var arr = $.toJSON(array);

    var a = "list=" + arr;


    $.ajax({
    url: filename,
    context: a,
    success: function(){alert(a);}

    });

    });
    </script>

    <?php

    $list = json_decode($_POST['list']);

    var_dump($list);
    ?>

    It says Undefined index: list.
     
    Solved! View solution.
    sergey6116, Sep 13, 2011 IP
  2. #2
    I haven't tried running your code, but try

    
    $.ajax({
        url: filename,
        data: "arr=a",
        success: function(){alert(a);
    }
        
    });
    
    Code (markup):
    BTW, the default is a GET. If you want POST data, add
    type: "POST",
    in the argument list. (Order doesn't matter, but the last item - success, in your case - doesn't end in a comma.)
     
    Rukbat, Sep 13, 2011 IP
  3. sergey6116

    sergey6116 Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks mate the type: "POST" helped :). Topic closed, everything works :).
     
    sergey6116, Sep 13, 2011 IP
  4. vruvishal

    vruvishal Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    var str='var1[]='+value1+'&var1[]='+value2;
    $.ajax({
    url:filename.php,
    data:str,
    method:'POST',
    success:function(msg)
    {
    alert(msg);
    }


    });
     
    vruvishal, Sep 28, 2011 IP