简体   繁体   English

使用ajax调用来调用C#[WebMethod]函数

[英]Call C# [WebMethod] function using an ajax call

I'm trying to call a function from a C# file in my custom module using an ajax call. 我正在尝试使用ajax调用从自定义模块中的C#文件中调用函数。 I have a .js file that uses an ajax call to whitelist.aspx/isValidURL (I've also tried whitelist.cs as the file extension and url without any luck). 我有一个.js文件,该文件使用ajax调用whitelist.aspx / isValidURL(我也尝试过whitelist.cs作为文件扩展名和url,但没有任何运气)。 I need to access the server method isValidURL, passing in the requested url, to see if the requested url is in a list of valid URLs. 我需要访问服务器方法isValidURL,传入请求的URL,以查看请求的URL是否在有效URL列表中。 If the requested url is in the list then I want to return true to the javascript file and otherwise return false. 如果请求的URL在列表中,则我想对JavaScript文件返回true,否则返回false。 Is this even possible? 这有可能吗? Below is the code I have thusfar: 下面是我到目前为止的代码:

Javascript code: JavaScript代码:

$(document).ready(function () {
    function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
            vars[key] = value;
        });
        return vars;
    }

    if (getUrlVars()["url"].indexOf("http") > -1) {
        var urlArray = getUrlVars()["url"].split('/');
        //var protocol = urlArray[0];
        var transferurl = urlArray[2];
    } else {
        var transferurl = getUrlVars()["url"];
    }

    $.ajax({
        type: "GET",
        url: "whitelist.aspx/isValidURL",
        data: {url:transferurl},
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (data) {
        if (data) {
            //Redirect to the requested url because it was a valid url in the whitelist
            setTimeout(function () { window.location.assign(transferurl); }, 5000);
        } else {
            //Don't redirect to the requested url because it wasn't a valid url in the whitelist
        }
    }).fail(function () {
        //Don't redirect to the requested url because there was an error looking it up in the whitelist
    });
});

C# Code: C#代码:

public class whitelist
    {
        [WebMethod]
        public static bool isValidURL(string requestedURL)
        {
            //Create a list of strings to contain all the "valid" URLs
            var whiteList = new List<string>();
            //Add URLs to the list
            whiteList.Add("www.google.com");

            foreach (string validURL in whiteList)
            {
                if (requestedURL == validURL)
                {
                    return true;
                }
            }

            return false;

        }
    }
  1. Create a new ASPX page named whitelist.aspx and put your webmethod in that. 创建一个名为whitelist.aspx的新ASPX页面,并将您的Web方法放入其中。
  2. stringify() your parameters and use POST instead of GET: stringify()您的参数,并使用POST而不是GET:

      $.ajax({ type: "POST", data: JSON.stringify({ requestedURL: transferurl }), ... 
  3. The return value may be wrapped, in which case you will access it as follows: 返回值可以包装,在这种情况下,您将按以下方式访问它:

    .done(function (data) { if (data.d) { ... } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM