dtzjvj3915 2013-06-10 22:43
浏览 10
已采纳

AJAX传递给PHP错误

I am passing serialized data and a variable as well to php, but i am getting an unexpected token '(' error in the data section

if i take out the 'lid':userID from the data section, it works fine.

$("#cname").submit(function (e) {
    e.preventDefault();
    $.ajax({
        url: 'someurl',
        crossDomain: true,
        type: 'post',
        data: {$("#cname").serialize(), 'lid':userID},
        success: function (data) {
            $("#result").html(data);
            $.mobile.changePage($('#page-tasklist'));
        },
    });
});
  • 写回答

2条回答 默认 最新

  • dqv84329 2013-06-10 22:50
    关注

    The output of the .serialize() method is a string like action=login&UserName=&Password=&Site=Test. So try this instead.

    $("#cname").submit(function (e) {
        e.preventDefault();
        $.ajax({
            url: 'someurl',
            crossDomain: true,
            type: 'post',
            data: $("#cname").serialize() + '&lid=' + userID,
            success: function (data) {
                $("#result").html(data);
                $.mobile.changePage($('#page-tasklist'));
            },
        });
    });
    

    Also, if you just remove lid: userID from your ajax request, you would actually still get an error because {$("#cname").serialize()} is not valid json. The data parameter can accept either an encoded string like what .serialize() returns or a json object.

    The following 2 examples are equivalent:

    $.ajax({url:'index.php',type:'POST',data:{z:1,y:2}});
    $.ajax({url:'index.php',type:'POST',data:'x=1&y=2'});
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度