使用AJAX的功能引发了“Unexpected Token}”,Chrome控制台

时间:2013-10-11 10:17:44

标签: javascript php jquery ajax

这是非常不正常的。我试图解决这个错误3天,但现在没有让我紧张。所以,我有一个按钮和一个文本框,用于将文本框中的数据提交到PHP页面。数据存储在我的数据库中。

这是PHP文件:

if(Class_Name::Function_Name($_POST["id"], $_POST["text"])) $data["status"] = "success";
else $data["status"] = "error";

echo json_encode($data);

这是JS函数:

function editPostOffer(postId, offer_id, edited_content) {
    $.post("admin/post_offer/edit_post_offer.php", {id: offer_id, text: edited_content.value}, function(data) {
        if(data.status == "success") {
            window.location.href = "post.php?id="+postId;
        } else {
            console.log("A");
        }
    }, 'json');
}

这是按钮+ textarea(以生成的形式):

<div class="content">
    <p class="content" style="display: none;">areebaaa</p>
    <textarea class="hide span12" id="edited_content" style="display: inline-block;">areebaaa</textarea>
    <button type="button" onclick="editPostOffer(" 13 ",=" " "56 ",=" " "assa ");"="" style="display: inline-block;">Save Edits</button>&nbsp;
    <button type="button" class="hide btn btn-primary btn-small cancel_edits" style="display: inline-block;">Cancel Edits</button>
</div>

但最初创建HTML的JS就在这里:

var offers = <? php echo Class_Name::Function_Name($_GET["id"]); ?> ;
for (var i = 0; i < offers.length; i++) {
    var date = offers[i].Date.split(" ");
    document.write('<div class="row-fluid offer">' +
        '<div class="span2">' +
        '<img class="profile_picture" src="' + offers[i].Picture_Path + '" />' +
        '</div>' +
        '<div class="span10">' +
        '<div class="row-fluid">' +
        '<div class="username">' +
        '<p style="font-weight: bold;">' + offers[i].Name + '</p>' +
        '</div>' +
        '</div>' +
        '<div class="row-fluid">' +
        '<div class="content">' +
        '<p class="content">' + offers[i].Text + '</p>' +
        '<textarea class="hide span12" id="edited_content">' + offers[i].Text + '</textarea>' +
        '<button type="button" class="hide btn btn-primary btn-small" id="save_edits" onclick=' + editPostOffer("13", "56", "assa") + '>Save Edits</button>&nbsp;' +
        '<button type="button" class="hide btn btn-primary btn-small cancel_edits">Cancel Edits</button>' +
        '</div>' +
        '</div>' +
        '<div class="row-fluid">' +
        '<div class="date">' +
        '<p class="pull-right"><strong><span class="muted">Offered on: </span></strong>' + date[0] + '</p>' +
        '</div>');
    if (offers[i].Username == "<?php echo $_SESSION["
    username "]; ?>") {
        document.write('<div class="controls pull-right">' +
            '<a href="" class="no_link edit_offer">Edit</a>&nbsp;' +
            '<a href="" class="no_link" onclick="showDeleteOfferModal(' + offers[i].Offer_ID + ');">Delete</a> |&nbsp;' +
            '</div>');
    }
    document.write('</div>' +
        '</div>' +
        '</div>' +
        '<hr />');
}

我知道错误在这里:.. onclick="editPostOffer(" 13 ",=" " "56 ",=" " "assa ");"=""..

有一个EQUALTOS和COMMAS的网格,但我不明白他们为什么要来?有什么方法可以解决这个问题吗?

更新

我已经更新了我的代码现在只出现的错误是JS无法找到 textarea

...
'<div class="content">' +
    '<p class="content">' + offers[i].Text + '</p>' +
    '<textarea class="hide span12" id="edited_content">' + offers[i].Text + '</textarea>' +
    '<button type="button" class="hide btn btn-primary btn-small" id="save_edits" onclick=\'editPostOffer("<?php echo $_GET["id"]; ?>","' + offers[i].Offer_ID + '","' + $("#edited_content") + '");\'>Save Edits</button>&nbsp;' +
    '<button type="button" class="hide btn btn-primary btn-small cancel_edits">Cancel Edits</button>' +
    '</div>' +
...

这是更新JS:

function editPostOffer(postId, offer_id, edited_content) {
    console.log(postId);
    console.log(offer_id);
    console.log(edited_content);
    $.post("admin/post_offer/edit_post_offer.php", {id: offer_id, text: edited_content.val()}, function(data) {
        if(data.status == "success") {
            window.location.href = "post.php?id="+postId;
        } else {
            console.log("A");
        }
    }, 'json');
}

当我将数据输出到控制台时,会出现:

13
56
Uncaught TypeError: Object [object Object] has no method 'val' 

最后一行中的错误意味着该对象为null,这是因为jQuery无法找到textarea。

3 个答案:

答案 0 :(得分:3)

不清楚你要传递给onclick函数的内容,请尝试使用不同的/转义引号:

onclick="editPostOffer(' 13 ',\'=' ' '56 '\',\'=' ' 'assa '\');"

onclick="editPostOffer(' 13 ',=' ' '56 ',=' ' 'assa ');"

修改

在您的帖子中添加了更多代码后,我建议您:

onclick="' + editPostOffer("13", "56", "assa") + '">
        ^                                         ^ //added quotes here

关于文本区域,您传递的是id,所以也许你应该尝试这个:

{id: offer_id, text: $('#'+edited_content).val()}

{id: offer_id, text: document.getElementById('edited_content').value}

而不是

{id: offer_id, text: edited_content.value}

答案 1 :(得分:0)

更改您的JS HTML创建行:

'<button type="button" class="hide btn btn-primary btn-small" id="save_edits" onclick=' + editPostOffer("13", "56", "assa") + '>Save Edits</button>&nbsp;' +

为:

'<button type="button" class="hide btn btn-primary btn-small" id="save_edits" onclick=\'editPostOffer("13", "56", "assa")\'>Save Edits</button>&nbsp;' +

答案 2 :(得分:0)

在你的JavaScript中添加onclick onclick=' + editPostOffer("13", "56", "assa") + '>时,它只是一个字符串,无需将其从字符串中删除。

尝试这样做 - onclick="editPostOffer(\"13\", \"56\", \"assa\")" >