From 0695a246f45a57550dc84e1fa5de47b243b1a60d Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Thu, 6 May 2021 18:41:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CorsClient/CorsClient/wwwroot/Index.html | 45 +++++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/CorsClient/CorsClient/wwwroot/Index.html b/CorsClient/CorsClient/wwwroot/Index.html index 26737c1..759ec4d 100644 --- a/CorsClient/CorsClient/wwwroot/Index.html +++ b/CorsClient/CorsClient/wwwroot/Index.html @@ -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 @@