Javascript 运行node.js文件时出现语法错误

Javascript 运行node.js文件时出现语法错误,javascript,ajax,node.js,syntax-error,Javascript,Ajax,Node.js,Syntax Error,伙计们,我正在使用Editinplace功能,在运行时,我在chrome控制台Uncaught SyntaxError:Unexpected tokenOK上遇到了这个错误,下面是发生的情况: 浏览器加载index.html 用户编辑字段并单击“保存” saveChanges向/save 您的服务器代码发送一个带有302状态代码和jsonp主体的HTTP响应 我认为浏览器正在透明地处理302状态代码,而忽略了主体 因此,jquery代码期望javascript来自/save的响应体,但它实际上

伙计们,我正在使用Editinplace功能,在运行时,我在chrome控制台
Uncaught SyntaxError:Unexpected tokenOK上遇到了这个错误,下面是发生的情况:

  • 浏览器加载index.html
  • 用户编辑字段并单击“保存”
  • saveChanges
    /save
  • 您的服务器代码发送一个带有302状态代码和jsonp主体的HTTP响应
  • 我认为浏览器正在透明地处理302状态代码,而忽略了主体
  • 因此,jquery代码期望javascript来自
    /save
    的响应体,但它实际上是从
    /save.HTML
    获取HTML。这就是语法错误发生的地方,当jquery试图将该HTML作为javascript进行评估时,因为您告诉它
    数据类型是
    jsonp
另见


解决方案是,您需要发送一个200响应代码,以便jquery可以执行jsonp操作,然后您可以根据需要将
window.location
更改为
/save.html

好的,下面是发生的情况:

  • 浏览器加载index.html
  • 用户编辑字段并单击“保存”
  • saveChanges
    /save
  • 您的服务器代码发送一个带有302状态代码和jsonp主体的HTTP响应
  • 我认为浏览器正在透明地处理302状态代码,而忽略了主体
  • 因此,jquery代码期望javascript来自
    /save
    的响应体,但它实际上是从
    /save.HTML
    获取HTML。这就是语法错误发生的地方,当jquery试图将该HTML作为javascript进行评估时,因为您告诉它
    数据类型是
    jsonp
另见

解决方案是,您需要发送一个200响应代码,以便jquery可以执行jsonp操作,然后您可以根据需要将
window.location
更改为
/save.html

case '/':
res.writeHead(302,{'location':'http://localhost/editinplace/index.html'});
res.end();
break;
case '/save':
console.log("called");

    console.log("Inside called");
    res.write('_testcb(\'{"message": "Hello world!"}\')');
res.writeHead(302,{'location':'http://localhost/editinplace/save.html'});

res.end();
break;
<script type="text/javascript">
$(document).ready(function(){
setClickable();
});
function setClickable() {
$('#editInPlace').click(function() {
var textarea = '<div><textarea rows="10" cols="60">'+$(this).html()+'</textarea>';
var button = '<div><input type="button" value="SAVE" class="saveButton" /> OR <input type="button" value="CANCEL"class="cancelButton" /></div></div>';
var revert = $(this).html();
$(this).after(textarea+button).remove();
$('.saveButton').click(function(){saveChanges(this, false);});
$('.cancelButton').click(function(){saveChanges(this, revert);});
})
.mouseover(function() {
$(this).addClass("editable");
})
.mouseout(function() {
$(this).removeClass("editable");
});
};//end of function setClickable
function saveChanges(obj, cancel) {
if(!cancel) {
var t = $(obj).parent().siblings(0).val();
var data=t;
 $.ajax({
        url: 'http://localhost:9090/save',
        type:"GET",
        dataType: "jsonp",
        jsonpCallback: "_testcb",
        cache: true,
        timeout: 1000,
        data:{data:JSON.stringify(data)},
        success: function(data) {
        },
        error: function(jqXHR, textStatus, errorThrown) {
        }
    });
}
else {
var t = cancel;
}
$(obj).parent().parent().after('<div id="editInPlace">'+t+'</div>').remove() ;
}
</script>
</head>
<body>
<div id="editInPlace">Nilesh</div>
</body>