在新标签页中以发布数据作为标题打开azure Web应用程序URL

巨无霸

我有一个Azure Web应用程序,我想在新选项卡中将帖子数据作为标题打开。我使用下面的代码使用JavaScript,该代码成功打开了azure Web URL,但错误为“由于使用了无效的方法(HTTP动词),因此无法显示正在查找的页面”,而不同的是,当我刷新它打开的页面

$scope.openurl= function {
const URL = 'https://xyz.azurewebsites.net';
                const _data = {
                    token: "54165165165"
                };
                submit_post_via_hidden_form(URL, _data);
        };

        function submit_post_via_hidden_form(url, params) {
            var f = $("<form target='_blank' method='post' style='display:none;' id='form1'></form>").attr({
                action: url
            }).appendTo(document.body);

            for (var i in params) {
                if (params.hasOwnProperty(i)) {
                    $('<input type="hidden" />').attr({
                        name: i,
                        value: params[i]
                    }).appendTo(f);
                }
            }

            f.submit();

            f.remove();
        }
潘杰森

更新

由于您的项目没有服务器端代码。我建议您将数据放入其中localstorage,当打开页面时可以使用它们$(document).ready(function(){ ###read data here from localstorgae### })

尊贵的

根据您的描述,我知道问题的原因。首先,因为您发送了发布请求以打开网页并通过表单主体传输数据。然后,您的后端程序需要处理此请求。

解决方法如下。您可以下载我的演示(.Net Core 3.1)

启动文件

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        // Enable AllowSynchronousIO 
        services.Configure<IISServerOptions>(options =>
        {
            options.AllowSynchronousIO = true;
        });
    }

HomeController.cs

    public IActionResult Index()
    {
        StreamReader stream = new StreamReader(HttpContext.Request.Body);
        string body = stream.ReadToEnd();
        ViewData["id2"] = body;
        return View();
    }

Index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    <p>Test Data</p>
    <p>@ViewData["id2"]</p>
</div>

并且您还需要在项目中添加一个类。

ReadableBodyStreamAttribute.cs

using Microsoft.AspNetCore.Authorization;
// For ASP.NET 3.1
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;

namespace defaultPost
{
    public class ReadableBodyStreamAttribute : AuthorizeAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            // For ASP.NET 3.1
            context.HttpContext.Request.EnableBuffering();
        }
    }
}

Mytest.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="Scripts/jquery.js"></script>
    <title>Document</title>
</head>
<script type="text/javascript">
    $(document).ready(function(){
        const URL = 'https://panshubeiweb.azurewebsites.net/';
                const _data = {
                    token: "54165165165"
                };
                openPostWindow(URL, _data);
    })

    function openPostWindow(url, params) {

      var newWin = window.open(),
            formStr = '';
       formStr = '<form style="visibility:hidden;" method="POST" action="' + url + '">' +
        '<input type="hidden" name="params" id="form2" value="' + params + '" />' +
        '</form>';
     newWin.document.body.innerHTML = formStr;
     newWin.document.forms[0].submit();

     return newWin;
  }
</script>
<body>  
</body>
</html>

您可以从github下载我的演示并对其进行测试。您还需要string body = stream.ReadToEnd();根据业务序列化正文字符串()。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章