Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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
Javascript jquery ajax json解析错误有效的json数据_Javascript_Jquery_Ajax_Json_Servlets - Fatal编程技术网

Javascript jquery ajax json解析错误有效的json数据

Javascript jquery ajax json解析错误有效的json数据,javascript,jquery,ajax,json,servlets,Javascript,Jquery,Ajax,Json,Servlets,嗨,我正在使用jquery-servlet调用ajax。我迷路了。哦,请告诉我该怎么办。 来自servlet的数据是有效的json{success:true,SelectPartid:{partidAll:DA04321;}} 如果我放弃数据类型:“json”,这就可以了。但是我不能使用这个代码 如果数据是成功的{ 成功!!; //alertsuccess+data.SelectPartid.PartId; } 因此,请告知ajax $("#select1").change(function()

嗨,我正在使用jquery-servlet调用ajax。我迷路了。哦,请告诉我该怎么办。 来自servlet的数据是有效的json{success:true,SelectPartid:{partidAll:DA04321;}}

如果我放弃数据类型:“json”,这就可以了。但是我不能使用这个代码

如果数据是成功的{ 成功!!; //alertsuccess+data.SelectPartid.PartId; }

因此,请告知ajax

$("#select1").change(function() {
    dataString = "Optionselected=" + $("#select1 option:selected").text();
    alert("dataString : " + dataString);
    $.ajax({
        type: 'POST',
        url: 'SelectAjx',
        data: dataString,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        context: this,
        success: function(data, textStatus, jqXHR) {
            if (data.success) {
                alert("SUCCESS!!");
                //alert("success" + data.SelectPartid.partidAll);
            }
        },
        error: function(jqXHR, exception) {
            //alert('[jqXHR:' + jqXHR + '], [textStatus:' + textStatus + '], [thrownError:' + errorThrown + '])');
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
});
application/json解决这个问题真不敢相信我因为这行代码浪费了2个小时。ajax不会导致错误。它在servlet中

这是电话线

response.setContentTypetext/html;字符集=UTF-8

我只是想把它改成

response.setContentTypeapplication/json;字符集=UTF-8

然后错误就消失了。感谢上帝


-抄袭我的上一条评论,我相信这对某人会有用。

快速观察。DA04321;就是,;肯定在双引号内?你确定响应看起来像你给我们看的JSON吗?我已经在测试过了。这是一个有效的答案json@RGraham是的,它在双人床里面quotes@RGraham那个当有多个结果时用作分隔符。