Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jQueryAjax请求的困境,无法工作_Php_Javascript_Jquery_Ajax - Fatal编程技术网

Php jQueryAjax请求的困境,无法工作

Php jQueryAjax请求的困境,无法工作,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,基本上,我试图通过AJAX发布一条评论,然后将新评论加载到HTML中。我认为我的AJAX成功函数有问题。数据很好地添加到数据库中,但我仍然收到一个错误“对不起,意外错误。请稍后重试”。正如成功函数本身中设置的那样 JS: HTML: 您似乎正在将json发送回success函数,因此您应该添加一个数据类型: $.ajax({ //this is the php file that processes the data and send mail url: "main/ajax_c

基本上,我试图通过AJAX发布一条评论,然后将新评论加载到HTML中。我认为我的AJAX成功函数有问题。数据很好地添加到数据库中,但我仍然收到一个错误“对不起,意外错误。请稍后重试”。正如成功函数本身中设置的那样

JS:

HTML:


您似乎正在将json发送回success函数,因此您应该添加一个数据类型:

$.ajax({
    //this is the php file that processes the data and send mail
    url: "main/ajax_comment", 

    //GET method is used
    type: "POST",

    //pass the data         
    data: form_data,

    // data type
    dataType: 'json',       /* added */

    //success
    ...

如果这并不能解决问题,请检查
c
变量包含的内容,可能还会回显一些php警告,从而使返回的json无效。

可能的问题或解决方案:

(1)不要使用输入类型
提交
,而是使用类型
按钮
并删除表单
操作
值,以及
方法
,因为您在
$.ajax
请求中指定了它

(2)不要忘记在
$请求中包含
dataType
参数。ajax
请求如下:

  $.ajax({
        url: "main/ajax_comment", 
        type: "POST",       
        data: form_data,
        dateType: "json",
        success: function (c) { 
  ...
success: function (c) { 
   console.log(c);
   console.log(c.status);
...
(3)检查从
PHP
执行
控制台返回的数据。在success函数中记录
,并检查状态和其他类似值:

  $.ajax({
        url: "main/ajax_comment", 
        type: "POST",       
        data: form_data,
        dateType: "json",
        success: function (c) { 
  ...
success: function (c) { 
   console.log(c);
   console.log(c.status);
...
(4)确保javascript和json
对象的组合良好

success: function (c) { 
   console.log(c);
   console.log(c.status);
...