【问题标题】:C# & ASP.NET Core MVC : web scraper ajax sends null to controllerC# & ASP.NET Core MVC:web scraper ajax 将 null 发送到控制器
【发布时间】:2022-10-05 01:16:57
【问题描述】:

我正在尝试在 C# 中实现一个简单的屏幕抓取程序。当我运行应用程序并在输入框中输入一个 url 并单击提交按钮时,我执行一些 jQuery 以使用 Ajax 将输入的 url 发送到控制器。出于某种原因,控制器总是收到空值。

这是我认为的代码:

 <div id="message"></div>
 <input id="urlInput" type="text" placeholder="Enter URL" />
 <button id="submit">Submit</button> 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
 <script>
    $(document).ready(function () {

    $("#submit").click(function (e) {
        var urlValue = $("#urlInput").val();
        console.log(urlValue);
        $.ajax({
                type: "POST",
                url: "/Home/GetUrlSource",
                contentType: "application/json; charset=utf-8",
                data: '{"url":"' + urlValue + '"}',
                dataType: "html",
                success: function (result, status, xhr) {
                    ExtractData(result);
                    //GetUrlTelePhone(result);
                },
                error: function (xhr, status, error) {
                    $("#message").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
                }
            });
    });
 </script>

当我运行应用程序时,会显示视图。在输入框中,我输入以下内容:

https://www.zillow.com/homes/90-westwood-circle-roslyn-heights-ny-11577

当我单击提交按钮并查看浏览器控制台时,我看到了 url,因此我知道当 console.log 指令执行时 url 值是有效的。

这是控制器中的代码:

[HttpPost]
public string GetUrlSource(string url)
{
    url = url.Substring(0, 4) != "http" ? "http://" + url : url;
    string htmlCode = "";

    using (WebClient client = new WebClient())
    {
        try
        {
            htmlCode = client.DownloadString(url);
        }
        catch (Exception ex)
        {
        }
    }

    return htmlCode;
}

我在GetUrlSource 的第一行代码处设置了一个断点,当我查看变量 url 时,它有一个空值。

任何想法为什么会发生这种情况?我在 Mac 上运行 Visual Studio 2019。该应用程序是一个 .NET Core 应用程序。

提前致谢。

【问题讨论】:

  • @zSynopsis dataType 是预期的返回类型,而不是发送的内容,即 contentType
  • 尝试传递一个对象,以便 jquery 可以将其转换为 json:data: { url : urlValue },
  • 查看浏览器网络选项卡以查看其发送方式。
  • @freedomn-m 当我查看网络选项卡时,我可以在 Name 列中看到 GetUrlSource。当我单击 Payload 选项卡时,我看到了应该传递的 url。
  • @freedomn-m 我尝试按照建议更改为对象,但最终在控制器中仍为 null。

标签: c# jquery ajax asp.net-core-mvc


【解决方案1】:

尝试以下操作:

 $("#submit").click(function (e) {
            var urlValue = $("#urlInput").val();
            console.log(urlValue);

            $.ajax({
                type: "POST",
                url: "/Home/GetUrlSource",
                data: { url: urlValue },
                dataType: "html",
                success: function (result, status, xhr) {
                    ExtractData(result);
                    //GetUrlTelePhone(result);
                },
                error: function (xhr, status, error) {
                    $("#message").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
                }
            });
        });

上面的代码中有两个修复:

  1. data: { url: urlValue },
  2. 删除contentType: "application/json; charset=utf-8", 以使用默认值。默认为:application/x-www-form-urlencoded; charset=UTF-8

    这对我有用。如果不起作用,请尝试清理浏览器缓存。

【讨论】:

  • 清除浏览器!为什么我没有想到呢?它总是简单的事情。谢谢!
  • @JonathanSmall:这不仅是清除浏览器缓存。当我取消注释contentType: 'application/json; charset=utf-8', 行时,GetUrlSource(string url)url 参数始终为null。使用邮政方法会影响data 选项的内容如何发送到服务器,这也是contentType 类型变得重要的原因。
猜你喜欢
  • 2021-03-30
  • 2020-12-30
  • 2023-03-04
  • 2020-10-05
  • 2021-06-02
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多