You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
3.3 KiB
HTML
84 lines
3.3 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>CORS跨域 客户端</title>
|
|
<script type="text/javascript" src="Scripts/jquery-1.10.2.js"></script>
|
|
<script type="text/javascript" src="Scripts/jquery.cookie.js"></script>
|
|
<script>
|
|
$(function () {
|
|
$("#MenuLinks li").click(function () {
|
|
var ajaxUrl = $(this).attr("url");
|
|
var ajaxMethod = $(this).attr("requestMethod");
|
|
var ajaxContentType = $(this).attr("requestContentType");
|
|
var ajaDataType = $(this).attr("responseDataType");
|
|
var ajaxRequestData = $(this).attr("requestData");
|
|
if (ajaxContentType.indexOf("json") > 0) {
|
|
if (ajaxRequestData == "") {
|
|
ajaxRequestData = "";
|
|
}
|
|
else {
|
|
var jsonData = JSON.parse(ajaxRequestData)
|
|
ajaxRequestData = JSON.stringify(jsonData);
|
|
}
|
|
};
|
|
$.ajax({
|
|
//请求方法
|
|
type: ajaxMethod,
|
|
|
|
//请求地址
|
|
url: ajaxUrl,
|
|
|
|
//服务器响应数据格式
|
|
dataType: ajaDataType,
|
|
|
|
//请求数据格式
|
|
contentType: ajaxContentType,
|
|
|
|
//请求数据
|
|
data: ajaxRequestData,
|
|
|
|
//请求前
|
|
beforeSend: function (xhr) {
|
|
$("#ContentBox").html("请求中...");
|
|
},
|
|
|
|
//请求成功
|
|
success: function (jsonData) {
|
|
var isjson = typeof (jsonData) == "object" && Object.prototype.toString.call(jsonData).toLowerCase() == "[object object]" && !jsonData.length;
|
|
if (isjson) {
|
|
jsonData = JSON.stringify(jsonData);
|
|
}
|
|
$("#ContentBox").text(jsonData.toString());
|
|
},
|
|
|
|
//请求失败
|
|
error: function (xhr, status, error) {
|
|
$("#ContentBox").html("请求错误" + error);
|
|
},
|
|
|
|
//请求完成
|
|
complete: function (xhr, status) {
|
|
|
|
}
|
|
});
|
|
});
|
|
|
|
$("#MenuLinks li:first").click();
|
|
});
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div id="MenuLinks">
|
|
<ul>
|
|
<li requestMethod="GET" requestContentType="application/x-www-form-urlencoded;charset=UTF-8" requestData="" url="http://localhost:7050/api/Test/Ping">简单API</li>
|
|
<li requestMethod="GET" requestContentType="application/x-www-form-urlencoded;charset=UTF-8" requestData="" url="http://localhost:7050/api/Cors/Ping">全局跨域策略</li>
|
|
<li requestMethod="GET" requestContentType="application/x-www-form-urlencoded;charset=UTF-8" requestData="" url="http://localhost:7050/api/Cors/NoCors">不允许跨域</li>
|
|
<li requestMethod="PUT" requestContentType="application/json;charset=UTF-8" requestData="{}" url="http://localhost:7050/api/Cors/HasCors">单独明确可以跨域</li>
|
|
</ul>
|
|
</div>
|
|
<pre id="ContentBox">
|
|
默认内容
|
|
</pre>
|
|
</body>
|
|
</html> |