简体   繁体   English

如何获取Ajax返回函数并阻止文档下载onclick

[英]how do I get my Ajax to return a function and prevent my document from downloading onclick

I have been trying to figure this out for hours. 我已经尝试了好几个小时了。 I have the following document path on and anchor tag 我在以下文档路径和锚标记上

<a href="@link.DocumentPath" onclick="return checkAuth(@link.Id, @PasswordProtected)">@link.Title</a>

I do not want to process the href if the "checkAuth" function returns a false. 如果“ checkAuth”函数返回false,我不想处理href。 Here is the "checkAuth" code. 这是“ checkAuth”代码。

var checkAuth = function(id, PasswordProtected) {
                    var result = null;
                    var hostname = location.hostname;
                    var host = '@System.Configuration.ConfigurationManager.AppSettings["hostroot"]';
                    if (hostname == "localhost")
                        host = "";
                    if (PasswordProtected == "1"){
                        var pass = prompt("This document is password protected", "");

                        var response = $.ajax({

                            type: "GET",
                            url: host + "/Communities/DeCryptPwd/",

                            data: {"id": id, "password": pass},
                            success: function (data) {
                                alert(data);
                                if (data == "True")
                                    result = true;
                                if (data == "False")
                                    result = false;

                            },
                            error: function (errorData) { alert(errorData); }
                        });
}

I just don't know how to stop the processing of the document on the href and return a true ... continue process, or false -- stop processing. 我只是不知道如何停止对href上的文档的处理并返回true ... Continue process或false-停止处理。

Just in case you need it, here is the "sever side" code called by the .ajax 万一您需要它,这是.ajax调用的“服务器端”代码

public bool DeCryptPwd(int id, string password) {
    var encrypted = db.CommunityDocs.Where(x => x.Id == id).Select(x => x.Password).SingleOrDefault();

    /* Extract the bytes */
    byte[] hashBytes = Convert.FromBase64String(encrypted);
    /* Get the salt */
    byte[] salt = new byte[16];
    Array.Copy(hashBytes, 0, salt, 0, 16);
    /* Compute the hash on the password the user entered */
    var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 10000);
    byte[] hash = pbkdf2.GetBytes(20);
    /* Compare the results */
    for (int i = 0; i < 20; i++)
        if (hashBytes[i + 16] != hash[i])
            return false;

    return true;
}

You cannot return the result from an async function from the function that contains it. 您不能从包含异步函数的函数返回结果。 So you need to change the location in the success part and return true if not protected. 因此,您需要更改成功部分中的位置,如果不受保护,则返回true。

However the code will currently allow them to right-click and open in new window regardless 但是,该代码当前允许他们右键单击并在新窗口中打开,无论

function checkAuth = function(theLink) {
  var id = theLink.id,
      protected = theLink.getAttribute("pass") == "1",
      href = theLink.href,
      result = null,
      hostname = location.hostname,
      host = '@System.Configuration.ConfigurationManager.AppSettings["hostroot"]';
  if (hostname == "localhost")
    host = "";
  if (protected) {
    var pass = prompt("This document is password protected", "");

    var response = $.ajax({

      type: "GET",
      url: host + "/Communities/DeCryptPwd/",

      data: {
        "id": id,
        "password": pass
      },
      success: function(data) {
        alert(data);
        if (data == "True") {
          location = href;
        }
      },
      error: function(errorData) {
        alert(errorData);
      }
    });
    return !protected; // return false if protected
  }
<a href="@link.DocumentPath" onclick="return checkAuth(this)" id="@link.Id" data-pass="@PasswordProtected">@link.Title</a>

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

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