2013-06-27 82 views
-2

我想用ajax发送数据,但我得到一个语法错误,我不明白为什么... 我读了一些相同的问题,但我仍然不明白我的错在哪里...语法错误:简单的ajax请求

$.ajax({ 
    type: "POST", 
    url: "imprime_planning", 
    data: { 
     "test": "test" 
    }, 
    dataType: "JSON", 
    success: function(msg){ 
     alert(msg); 
     console.log(msg); 
    }, 
    error: function(res, status, exeption) { 
     alert(res); 
     console.log(exeption); 
    } 
}); 

感谢您的专家眼睛!

编辑: 了“的SyntaxError”在误差函数的异常中给定

+2

你在哪里得到语法错误?哪一行号码 – DaveHogan

+0

而且在代码中看不到任何语法错误。 –

+0

什么是语法错误...发布那些错误... – Hariharan

回答

0

我写这篇test.php的澄清:

test.php的:

<?php 
    $res = array(); 
    $res['answer'] = "got POST request with test: ".$_POST['test']; 
    print(json_encode($res)); 
?> 

Ajax调用:

$.ajax({ 
    type: "POST", 
    url: "test.php", 
    data: { 
     "test": "test" 
    }, 
    dataType: "JSON", 
    success: function(msg){ 
     alert(msg.answer); 
     console.log(msg.answer); 
    }, 
    error: function(res, status, exeption) { 
     alert(res); 
     console.log(res); 
    } 
}); 

适合我...

+0

感谢您的回答,我得到:{} 当我尝试:console.log(res.responseText); 我收到了我期待的内容,但是我的下一页没有收到发布数据 – ThEBiShOp

+0

您确定您不错过URL中的.php扩展名吗?你可以发布你如何试图获取下一页上的数据吗? – shak

+0

我正在使用cakephp,url被重写。 我使用调试($ this-> request-> data),它应该显示页面(post,get或其他)收到的数据,我得到一个空数组。 但是这是有道理的,因为我的ajax请求仍然抛出错误的功能,我猜... – ThEBiShOp

0

确保您设置Response.StatusCode比200以外的东西写使用的Response.Write您的异常的信息,然后使用...

xhr.responseText 

...在您的JavaScript。

请参考下链接,指引....

http://encosia.com/use-jquery-to-catch-and-display-aspnet-ajax-service-errors/

+0

我不明白发生了什么... 我在错误函数中显示res.responseText,它似乎是我所期望的,但是当我去在网页中的页面,我的POST是空的... – ThEBiShOp

+0

我不明白你对邮政有什么期待?你的问题正确无误! – Hariharan

+0

解决? 我没有收到帖子数据,我只是期待一个解释。 – ThEBiShOp