简体   繁体   English

将数据从jQuery Ajax发送到MVC COntroller

[英]Sending Data from jQuery Ajax to MVC COntroller

I am trying to send an SVG string from my index page to my controller. 我正在尝试将SVG字符串从索引页发送到控制器。 But it's getting a null value. 但是它得到一个空值。

var str = "<svg height=\"350\" version=\"1.1\" ... svg properties ..."
console.log(str);

$.ajax({
    type: "POST",
    //contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    //dataType: "text",
    url: window.location.origin + '/NavigationExport/GetSvgData',
    //data: '{"value":"' + str + '"}',
    data: {value :str},
}).done(function (data) {
    debugger
});

Here is my controller code. 这是我的控制器代码。

[HttpPost]
public void GetSvgData(string value)
{
    return;
}

but this code is giving me a 500 internal server error. 但是这段代码给了我500个内部服务器错误。

A potentially dangerous Request.Form value was detected from the client ( value="<svg height="350" ve..." ). 从客户端检测到一个潜在危险的Request.Form值( value="<svg height="350" ve..." )。

Description: ASP.NET has detected data in the request that is potentially dangerous because it might include HTML markup or script. 说明:ASP.NET在请求中检测到潜在危险的数据,因为它可能包含HTML标记或脚本。 The data might represent an attempt to compromise the security of your application, such as a cross-site scripting attack. 数据可能表示试图破坏应用程序的安全性,例如跨站点脚本攻击。 If this type of input is appropriate in your application, you can include code in a web page to explicitly allow it. 如果这种输入适合您的应用程序,则可以在网页中包含代码以明确允许它。 For more information, see http://go.microsoft.com/fwlink/?LinkID=212874 . 有关更多信息,请参见http://go.microsoft.com/fwlink/?LinkID=212874

If I use 如果我用

data: '{"value":"' + str + '"}'

it sends null value to the controller 它向控制器发送空值

The issue is because MVC thinks you're trying to send HTML in the request, and prevents it to stop any XSS attacks. 问题是因为MVC认为您正在尝试在请求中发送HTML,并阻止了它停止任何XSS攻击。 You can allow HTML/XML by adding the ValidateInput annotation to Action. 您可以通过将ValidateInput批注添加到Action来允许HTML / XML。 In your case, try this: 在您的情况下,请尝试以下操作:

[HttpPost]
[ValidateInput(false)]
public void GetSvgData(string value)
{
    return;
}

You can use [ValidateInput(false)] as action attribute in the controller to allow HTML string passing to action. 您可以在控制器中使用[ValidateInput(false)]作为操作属性,以允许将HTML字符串传递给操作。

    [ValidateInput(false)] 
    public ActionResult ActionName()
    {

    }

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

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