我无法进行客户端站点 AJAX 调用,然后使用 PageMethods 将 JSON 对象响应传递到服务器端以执行一些条件逻辑
从 Web 服务收到的 JSON 响应是:
{"Status":"Internal"}
默认.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GoRedirect._Default" %>
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<script type="text/javascript">
function invokeService() {
$(document).ready(function () {
$.ajax({
type: "GET",
async: "false",
url: "http://Domain.Local/InternalCheck/",
dataType: "json",
success: function (result) {
AjaxSucceeded(result);
},
error: AjaxFailed
});
});
}
function AjaxSucceeded(result) {
var objJSON = result.Status;
PageMethods.GetJSONResponse(objJSON);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
invokeService();
</script>
</form>
默认.aspx.cs
namespace GoRedirect
{
public partial class _Default : Page
{
[Serializable]
public class ServerJSON
{
public string Status { get; set; }
}
[WebMethod]
public static string GetJSONResponse(string objJson)
{
try
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string JSONResponse = serializer.Serialize(objJson);
return JSONResponse;
}
catch(Exception errorException)
{
return errorException.ToString();
}
}
public void Page_Load(object sender, System.EventArgs e)
{
If (JSONResponse.Status == "Internal")
{
//Do something
}
}
}
}