Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript的aspnet c表单提交_Javascript_Asp.net - Fatal编程技术网

使用javascript的aspnet c表单提交

使用javascript的aspnet c表单提交,javascript,asp.net,Javascript,Asp.net,我有一个aspnet web应用程序,它托管在本地iis上。在其他web应用程序中,我需要一个链接,当我单击该链接时,它应该打开我的托管web应用程序,而无需询问凭据。我用下面的代码来做,但它不工作 <html> <script type="text/javascript"> function OpenURL() { var form = document.createElement("form"); form.setAttribu

我有一个aspnet web应用程序,它托管在本地iis上。在其他web应用程序中,我需要一个链接,当我单击该链接时,它应该打开我的托管web应用程序,而无需询问凭据。我用下面的代码来做,但它不工作

<html>
<script type="text/javascript">
    function OpenURL() {
        var form = document.createElement("form");
        form.setAttribute("method", "POST");
        form.setAttribute("action", "http://10.10.10.10/Domain/Login.aspx");

        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "UserName");
        hiddenField.setAttribute("value", "UserTest"); 
        form.appendChild(hiddenField);

        var hiddenField2 = document.createElement("input");
        hiddenField2.setAttribute("type", "hidden");
        hiddenField2.setAttribute("name", "Password");
        hiddenField2.setAttribute("value", "PasswordTest");
        form.appendChild(hiddenField2);

        document.body.appendChild(form);
        form.submit();
    }
    </script>
<body onload="OpenURL();">
</body>
</html>

有人能帮我吗?

您可以尝试将实际的html标记放入您的文件中,然后重新加载。您只需提交表单,如下所示: 伪代码只是给人一个提示,不能测试它

<body onLoad="javascript:myform.submit();">
  <form name="myform"...>
  <input ...>
  <input ..>
  </form>
</body>

对所有输入元素尝试以下操作:

hiddenField.type = "hidden";
hiddenField.name = "UserName";
hiddenField.value = "UserTest"; 
而不是:

hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "UserName");
hiddenField.setAttribute("value", "UserTest"); 

我认为最好在托管的web应用程序上设置适当的端点,而不是直接使用javascript传递/分配凭据

您可以创建一个控制器方法,该方法模仿登录提交的功能,在托管的web应用程序上传递加密信息并对其进行解码,并在处理加密凭据后设置登录页。大概

http://10.10.10.10/Domain/LoginRedirect?data=your_encrypted_data_here

但是,如果您试图在托管的web应用程序上获取/保存/更新某些信息,则可以尝试创建api端点,并使用restful调用进行身份验证/执行。

现在,根据您的建议,我正在使用以下代码,但仍然不起作用。仅供参考-在我的目标web应用程序中,我使用了登录控制;