php - jquery ajax使用echo时json请求错误

标签 php jquery json request

我有这个代码:

$.ajax({
          url: 'urlsubmit.php',
          data: json_data,
          dataType: 'json',
          type: 'POST',
          success: function(data) {
                            $('#saveResult_'+tnTopic_id).html("OK");
            if(data.indexOf("ok") !== -1) {
                $("#saveResult_"+tnTopic_id).html("Ok, am salvat!");
            }
            else {
                $("#saveResult_"+tnTopic_id ).html(data);
                //alert("A aparut o eroare:\n\r"+data);
            }
          },
            error: function (jqXHR, textStatus, errorThrown) {
                    $('#saveResult_'+tnTopic_id).html(textStatus + ' | ' + errorThrown + ' | ' + jqXHR.responseText);
                    //alert(xhr.status);
                    //alert(thrownError);
                  }           
        }); 
}

在 urlsubmit.php 中我只回显“ok”

我得到了这个错误parsererror |语法错误:意外的标记 o

最佳答案

删除此 - (如果您没有使用 json 响应)

dataType: 'json',

jQuery 正在尝试将 ok 解析为 json,这就是您收到该错误的原因

关于php - jquery ajax使用echo时json请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17222721/

相关文章:

php - While 语句不显示 ECHO

php - fatal error : Uncaught exception 'PHPExcel_Exception'

php - json 从 mysql (使用 php) 到标签

javascript - beforeShowDay 每天调用两次

php - 将表单保存到 JSON

php - 使用 PHP 时查询仅返回一行

jquery - 设计 ajax 模态登录不适用于 Rails 5

javascript - jquery点击函数后保存当前状态

json - 使用 json.RawMessage 解码嵌套 json 字符串

javascript - 使用 Javascript 将 json 中的数字字符串化为十六进制(或其他)格式