2013-01-22 122 views
2

因此,我试图提交一个字符串作为参数在Js中发布到asp.net服务,而我有一些困难。在声明之前,我无法访问服务器,也无法触及验证,我严格从外部客户端访问。我得到这个响应返回从客户端检测到有潜在危险的Request.Form值,请编码帮助

System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (message="...t;img src='http://192.168.1..."). 
    at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection) 
    at System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, RequestValidationSource requestCollection) 
    at System.Web.HttpRequest.get_Form() 
    at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) 
    at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 

我发送的信息是:

xcvxzcvzxcvxcvzxcv< br /><img src='http://192.168.1.1:82/UserUploads/Images/65968/20130122020024996.jpg' alt='User Image' /> 

其中我用编码:

htmlEncode: function(str) { 
     str = str.replace(/&/g, '&amp;'); 
     str = str.replace(/'/g, '&#39;'); 
     str = str.replace(/"/g, "&quot;"); 
     str = str.replace(/</g, '&lt;'); 
     str = str.replace(/>/g, '&gt;'); 
     return str; 
    }, 

主要生产:

xcvxzcvzxcvxcvzxcv&lt; br /&gt;&lt;img src=&#39;http://192.168.1.1:82/UserUploads/Images/65968/20130122020802027.jpg&#39; alt=&#39;User Image&#39; /&gt; 

我已经通过了几个验证器并检查了我的en编码,我无法弄清楚是什么导致了这个问题。我唯一的猜测是,http://导致问题,因为它显示在JavaScript错误,但我不知道。任何帮助或见解将不胜感激。

+0

可以显示实际执行POST的代码吗? – PleaseStand

+1

这是'实体。看起来像任何&#..;编码在asp.net中被标记为危险。 – Ceres

+0

宾果,就是这样,换出'转换',它完美的作品 – knightsbore

回答

0

问题是编码'。根据用户409762,在asp.net中,&#的组合被标记为危险。

所以,现在我的编码看起来像这样,工作正常。

htmlEncode: function(str) { 
    str = str.replace(/&/g, '&amp;'); 
    str = str.replace(/"/g, "&quot;"); 
    str = str.replace(/</g, '&lt;'); 
    str = str.replace(/>/g, '&gt;'); 
    return str; 
}, 
0

使用jquery,你可以执行编码和解码like this link

function htmlEncode(value) { 
    return $('<div/>').text(value).html(); 
} 

function htmlDecode(value) { 
    return $('<div/>').html(value).text(); 
} 
相关问题