Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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
JSON对象无法通过jQuery Ajax和Java传递_Jquery_Ajax - Fatal编程技术网

JSON对象无法通过jQuery Ajax和Java传递

JSON对象无法通过jQuery Ajax和Java传递,jquery,ajax,Jquery,Ajax,我正在使用jQuery生成一个JSON对象,并尝试使用jQueryAjax传递它。但是,当使用console.log变量时,我获得了JSON对象格式的正确数据,当将变量作为数据传递时,我得到了如下异常 JSON.parse: unexpected character at line 2 column 1 of the JSON data 但我试图通过从console.log复制数据并硬编码为 data: '{"bookSeriesTitle":"test","bookEdition":"te

我正在使用jQuery生成一个JSON对象,并尝试使用jQueryAjax传递它。但是,当使用console.log变量时,我获得了JSON对象格式的正确数据,当将变量作为数据传递时,我得到了如下异常

JSON.parse: unexpected character at line 2 column 1 of the JSON data
但我试图通过从console.log复制数据并硬编码为

 data: '{"bookSeriesTitle":"test","bookEdition":"test","bookAuthorPrimary":"test","bookEditor":"test"}'
工作正常..以下是我的Jquery ajax代码:

console.log("'" + result + "'");
        $.ajax({ 
            url: "saveMe", 
            type: 'POST', 
            dataType: 'json', 
            data: "'" + result + "'" ,         
            //data: '{"bookSeriesTitle":"test","bookEdition":"test","bookAuthorPrimary":"test","bookEditor":"test"}', 
            contentType: 'application/json',
            mimeType: 'application/json',
            success: function(data) { 
                alert("success");
            },
            error:function(data,status,er) { 
                alert("error: "+data+" status: "+status+" er:"+er);
            }
        });

任何人都可以告诉我如何解决此问题…非常感谢您提前…

您需要使用JSON.stringify函数:

$.ajax({ 
        url: "saveMe", 
        type: 'POST', 
        dataType: 'json', 
        data: JSON.stringify(result),          
        contentType: 'application/json',
        mimeType: 'application/json',
        success: function(data) { 
            alert("success");
        },
        error:function(data,status,er) { 
            alert("error: "+data+" status: "+status+" er:"+er);
        }
});

在对象中传递数据时,不应将
{
}
括在
'
中。如果这样传递,它将是一个字符串

将数据作为

data: {
        //Your data
      }

我已经修改了数据:JSON.stringify(result),但仍然得到了相同的异常。。