json响应在{"userName":"clevermeal835","userRole":"Participant"}
我收到一条成功的警报消息,但在阅读响应时出现错误:
XMLHttpRequest 无法加载 url Origin null 是 Access-Control-Allow-Origin 不允许的。
如果我从命令提示符运行代码
--禁用网络安全
我得到回应。任何人都可以在不使用命令提示符的情况下帮助我解决这个问题。下面的代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="Scripts/jquery-1.4.2.min.js"></script>
<script>
$(document).ready(
function() {
$("#jsonpbtn2").click(function() {
var uid = "clevermeal835";
var pwd = "Welcome_1";
var userType = "participant";
var surl="http://localhost:8080/RESTlet_WS/MobiSignIn/{\"userName\":\""+uid+"\",\"password\":\""+pwd+"\",\"userType\":\""+userType+"\"}/";
$.ajax({
type : 'GET',
contentType: "application/json; charset=utf-8",
url : surl,
dataType : 'json',
headers : {Accept : "application/json","Access-Control-Allow-Origin" : "*"},
crossDomain : true,
success :SucceedFunc ,
error : function(data, textStatus, errorThrown) {
console.log("error"+' '+JSON.stringify(data) + textStatus + errorThrown);}
});
function SucceedFunc(data) {
alert("success");
var userName = data.userName;
alert(userName);
}
});
});
</script>
</head>
<body>
<input id="jsonpbtn2" type="submit" value="button" />
</body>
</body>
</html>