【问题标题】:How to pass a URL as parameter to APIController through AJAX call?如何通过 AJAX 调用将 URL 作为参数传递给 APIController?
【发布时间】:2016-10-08 12:14:19
【问题描述】:

我正在尝试将 URL 作为字符串参数传递给 API 控制器 POST 方法。但是,由于 URL 中的特殊字符“:”,我收到“潜在危险请求”错误。

URL 的格式为 'http|s://xxxxxxxxx/.../.... 等

我尝试了 encodeURI,但仍然失败。

$.ajax({
    type: "POST",
    url: "http://localhost:101/api/Sample/" + encodeURI(url),
    contentType: "application/json; charset=utf-8",
    dataType: "json"....
})

【问题讨论】:

    标签: ajax url asp.net-web-api parameters controller


    【解决方案1】:

    假设您要将数据发布到“http://localhost:101/api/Sample/”,您必须将 encodeURI(url) 添加到您的发布请求的请求正文中。比如这个。

    var formData = {urlParameter:encodeURI(url)}; //Json 
    $.ajax({
        url : "http://localhost:101/api/Sample/",
        type: "POST",
        data : formData,
        success: function(data, textStatus, jqXHR)
        {
            //handle success
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
             //Handle error
        }
    });
    

    您发布到的 Sample 方法,必须有一个参数对应于 formData 变量中 json 中指定的参数。

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      相关资源
      最近更新 更多