SyntaxError:意外的令牌C.

时间:2015-05-27 00:16:44

标签: jquery ajax json

我有以下ajax调用,它一直给出错误"语法错误:意外的令牌C" ,输出" sync.py:是 一个有效的JSON格式,如下所示....我在这里缺少什么?看了其他帖子,我几乎涵盖了列出的建议..如何修复此错误?请建议

            $.ajax({
                dataType: "json",
                type: "POST",
                contentType: "application/json",//note the contentType definition
                url: "scripts/sync.py",
                data: JSON.stringify(data_cp),
                //data: data_cp,
                error : function (xhr, ajaxOptions, thrownError){
                    console.log("cherypick fail");
                    console.log(response);      
                    console.log(response['returnArray']);
                    alert(xhr.status);
                    alert(thrownError); 
                },
                success: function(response){
                    console.log("cherypick sucess");
                    console.log(response);
                    console.log(response['returnArray']);                       
                }
            });

错误: -

SyntaxError: Unexpected token C

输出sync.py,它是有效的JSON格式

Content-Type: application/json


{"message": "The Command Completed Successfully", "returnArray": {"faillist": [], "picklist": ["1258565", "1279604"]}, "success": "true"}

1 个答案:

答案 0 :(得分:1)

这是因为您在响应正文中返回Content-Type: application/json,它应该是HTTP标头。

如果你这样做,你会得到同样的错误:

JSON.parse('Content-Type: application/json {"message": "The Command Completed Successfully", "returnArray": {"faillist": [], "picklist": ["1258565", "1279604"]}, "success": "true"}');
//SyntaxError: Unexpected token C

您应该更改sync.py脚本以仅返回响应正文中的JSON。那就是:

{"message": "The Command Completed Successfully", "returnArray": {"faillist": [], "picklist": ["1258565", "1279604"]}, "success": "true"}