如何在jquery.ajax()中解决[object Object]错误

时间:2014-01-31 11:07:44

标签: javascript php jquery ajax

我使用下面的jquery函数从表单发送数据并获得回复。下面是我用来获得结果的Jquery函数:

$(document).ready(function(){
    console.log( "ready!" );
    $("#button").click(function(){
        console.log("ready2");
        $.ajax({
            statusCode: {
                500: function() {
                    alert("error");
                }
            },
            url: './contact.php',
            type: "POST",
            data: {
                "name": $("#name").val(),
                "element_4_1": $("#element_4_1").val(),
                "element_4_2": $("#element_4_2").val(),
                "element_4_3": $("#element_4_3").val(),
                "email": $("#email").val(),
                "input4": $("#input4").val(),
            },
            error: function(t){
                console.log("Error: "+t);
            },
            success: function(data){
                $("#stage").text("Thank You");
            }
        });
    });
});  

现在这里的文本表单发送数据:

<div class="col-lg-7 well">
    <h4 style="padding-top:8px;">Your email address will not be published. Required fields are marked <font color="red">*</font></h4>
    <label>Name<font color="red">*</font></label><br>
    <input class="form-control" style="height:35px;width:230px;border-radius:4px;" required type="text" name="name"/><br>
    <label>Phone<font color="red">*</font></label><br>
    <span>
        <input id="element_4_1" name="element_4_1" class="element text" size="3" maxlength="3" value=""  type="text"> -
    </span>
    <span>
        <input id="element_4_2" name="element_4_2" class="element text" size="4" maxlength="4" value="" type="text"> -
    </span>
    <span>
        <input id="element_4_3" name="element_4_3" class="element text" size="10" maxlength="10" value=""  type="text" required >
    </span>
    <br><br>
    <label>Email<font color="red">*</font></label><br>
    <input id="email" class="form-control" style="height:35px;width:230px;border-radius:4px;" required type="email" name="text"/><br>
    <label for="input4">Message</label>
    <textarea name="contact_message" class="form-control" rows="4" id="input4"></textarea>
    <p>&nbsp;</p>
    <button type="submit" style="margin-left:65px;"class="btn btn-large btn-info" id="button">Submit</button>   
</div>

我打电话的contact.php是这样的:

<?php
if( $_REQUEST[name] &&
    $_REQUEST[element_4_1] &&
    $_REQUEST[element_4_2] &&
    $_REQUEST[element_4_3] &&
    $_REQUEST[email] &&
    $_REQUEST[message]){
   echo " Thank You ";
}
?> 

调用函数时出现此错误:jquery:-[object Object] error。我怎么称呼它。

1 个答案:

答案 0 :(得分:3)

不要将对象强制转换为字符串:

console.log("Error: "+t);

将对象调试为对象:

console.log("Error: ", t);