|
|
|
@ -9,8 +9,14 @@
|
|
|
|
|
$(function () {
|
|
|
|
|
$("#MenuLinks li").click(function () {
|
|
|
|
|
var ajaxUrl = $(this).attr("url");
|
|
|
|
|
var ajaxMethod = $(this).attr("method");
|
|
|
|
|
var ajaxContentType = $(this).attr("contentType");
|
|
|
|
|
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) {
|
|
|
|
|
var jsonData = JSON.parse(ajaxRequestData)
|
|
|
|
|
ajaxRequestData = JSON.stringify(jsonData);
|
|
|
|
|
};
|
|
|
|
|
$.ajax({
|
|
|
|
|
//请求方法
|
|
|
|
|
type: ajaxMethod,
|
|
|
|
@ -18,23 +24,36 @@
|
|
|
|
|
//请求地址
|
|
|
|
|
url: ajaxUrl,
|
|
|
|
|
|
|
|
|
|
//服务器返回类型
|
|
|
|
|
dataType: "html",
|
|
|
|
|
//服务器响应数据格式
|
|
|
|
|
dataType: ajaDataType,
|
|
|
|
|
|
|
|
|
|
//发送类型
|
|
|
|
|
//请求数据格式
|
|
|
|
|
contentType: ajaxContentType,
|
|
|
|
|
|
|
|
|
|
//data: JSON.stringify({ "userName": "wanggaofeng", "userPassword": "213464" }),
|
|
|
|
|
//请求数据
|
|
|
|
|
data: ajaxRequestData,
|
|
|
|
|
|
|
|
|
|
//请求前
|
|
|
|
|
beforeSend: function (xhr) {
|
|
|
|
|
$("#ContentBox").html("请求中...");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//请求成功
|
|
|
|
|
success: function (jsonData) {
|
|
|
|
|
$("#ContentBox").html(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) {
|
|
|
|
|
|
|
|
|
|
//请求失败
|
|
|
|
|
error: function (xhr, status, error) {
|
|
|
|
|
$("#ContentBox").html("请求错误" + error);
|
|
|
|
|
},
|
|
|
|
|
complete: function (xhr,status) {
|
|
|
|
|
|
|
|
|
|
//请求完成
|
|
|
|
|
complete: function (xhr, status) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -47,10 +66,10 @@
|
|
|
|
|
<body>
|
|
|
|
|
<div id="MenuLinks">
|
|
|
|
|
<ul>
|
|
|
|
|
<li method="GET" dataType="json" contentType="application/x-www-form-urlencoded" url="http://localhost:7050/api/Test/Ping">简单API</li>
|
|
|
|
|
<li method="GET" dataType="json" contentType="application/x-www-form-urlencoded" url="http://localhost:7050/api/Cors/Ping">全局跨域策略</li>
|
|
|
|
|
<li method="GET" dataType="json" contentType="application/x-www-form-urlencoded" url="http://localhost:7050/api/Cors/NoCors">不允许跨域</li>
|
|
|
|
|
<li method="PUT" dataType="json" contentType="application/x-www-form-urlencoded" url="http://localhost:7050/api/Cors/HasCors">单独明确可以跨域</li>
|
|
|
|
|
<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">
|
|
|
|
|