I'm trying to call a function from a C# file in my custom module using an ajax call. 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). 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. If the requested url is in the list then I want to return true to the javascript file and otherwise return false. Is this even possible? Below is the code I have thusfar:
Javascript code:
$(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:
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;
}
}
stringify()
your parameters and use POST instead of GET:
$.ajax({ type: "POST", data: JSON.stringify({ requestedURL: transferurl }), ...
The return value may be wrapped, in which case you will access it as follows:
.done(function (data) { if (data.d) { ... } })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.