在做前后端分离的项目中,当时在做使用easyUI做添加数据的时候,希望后台有返回值给前台接收来记录,可是执行方法之后发现没有返回值。
打开浏览器检查出现警告,如下(这是由于跨域接收返回的json数据问题)
Resource interpreted as Document but transferred with MIME type application/json
翻译:资源被解释为文档,但使用MIME类型application/json传输
我的解决方法是换一种提交方式:
当时通过$('#ff').form()
进行提交:
$("#btn").click(function() {
$("#ff").form("enableValidation");
if ($("#ff").form("validate")) {
$('#ff').form('submit', {
url : 'http://127.0.0.1:10011/si/service/service/insert',
onSubmit : function() {
return true;
},
success : function(count) {
var count = $.parseJSON(count);
//可以定义为对应消息框
if (count > 0) {
alert(count);
alert("添加成功!");
} else {
alert(count);
alert("添加失败!");
}
parent.$("#win").window("close");
win.$("#dg").datagrid("reload");
}
});
}
});
换成$.post();
方式进行提交,问题解决
$("#btn").click(function() {
$("#ff").form("enableValidation");
var url ="http://127.0.0.1:10011/si/service/service/insert";
var sendData = $("#ff").serialize();//提交form表单数据
$.post(url, sendData, function (backData, textStatus, ajax) {
if(textStatus){
alert(ajax.responseText);
alert("添加成功!");
}else{
alert("添加失败!");
}
parent.$("#win").window("close");
win.$("#dg").datagrid("reload");
});
});