简体   繁体   English

使用URL Encode / Decode从用户输入到HTML5数据属性的转义/特殊字符

[英]Escape/Special Characters from user input to HTML5 data-attributes using URL Encode/Decode

This is my first question on stackoverflow - needing help from all you experts. 这是我关于stackoverflow的第一个问题 - 需要所有专家的帮助。 Our web app allows the user to input escape/special characters. 我们的网络应用程序允许用户输入转义/特殊字符。 We are doing some testing for extreme cases. 我们正在为极端情况做一些测试。 I'm able to pass the escape characters through ajax/json. 我可以通过ajax / json传递转义字符。

 var test = JSON.stringify({ typeName: $("#typeName").val(), invoiceType: 'NewTypeArea' }); $.ajax({ type: "POST", url: "BackEnd/WebService.php", data: { newInvoice: test }, dataType: 'json' }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 

My backend developer does their thing with escape/special characters. 我的后端开发人员用转义/特殊字符做他们的事情。 When I make another call I get the following JSON data 当我再次打电话时,我得到以下JSON数据

[{"propName":"Special '\"'\\","typeName":"type'\"\\"}]

This data returned is correct. 返回的数据是正确的。 For simplicity - I have only one object for this example - I take this data and put it into a dropdown list. 为简单起见 - 本例中我只有一个对象 - 我将这些数据放入下拉列表中。

$.each(data, function(i, val) {
var output = '<option class="type" id="' + this.qfInvoiceNum + '" value="' + this.typeName + '">' + this.typeName +'</option>';
$('select#typeList').append(output);

}); });

It displays all the escape/special characters correctly. 它正确显示所有转义/特殊字符。 For instance: "type'\\"\\" displays as type'\\"\\ 例如:“type'\\”\\“显示为类型'\\”\\

The user then selects one of the list items. 然后,用户选择一个列表项。 I've tried capturing the typeName numerous ways with data-attributes, value, etc. Here is one example: 我尝试使用数据属性,值等捕获typeName的许多方法。这是一个例子:

$( '.selectChange1').change(function() {
typeGrab = $('select option.type:selected').val();
pullArea();
});

But this is where the problem comes in. If they select a list item with special/escape characters the value from the console returns 但这就是问题所在。如果他们选择带有特殊/转义字符的列表项,控制台的值将返回

"type'"

not

"type'\"\\"

So of course when I pass in the var typeGrab for the JSON/AJAX call it is wrong. 当然,当我传入var typeGrab进行JSON / AJAX调用时,这是错误的。

Is there a good solution on on how to get escape/special characters from a dropdown? 关于如何从下拉菜单获取转义/特殊字符有一个很好的解决方案吗? Or maybe I'm going wrong with $.each when I loop through the data? 或者,当我遍历数据时,我可能会遇到$ .each? Or, quite possibly, I'm missing something very simple. 或者,很可能,我错过了一些非常简单的事情。 Any help or insight would be great. 任何帮助或见解都会很棒。

Thank you. 谢谢。

Additional Info. 附加信息。 The first solution does work. 第一个解决方案确实有效。 However, I didn't use this initially due to the fact that there are a couple of times where the displayed text is not what I want to grab - which is why I was trying data*-attributes. 但是,我最初没有使用它,因为有几次显示的文本不是我想要抓取的 - 这就是我尝试数据* -attributes的原因。

For example: 例如:

$.each(data, function(i, val) {
var output = '<option id="' + this.qfInvoiceNum + '" class="type" data-index="' + this.typeName + '" >' + this.typeName +' | #' + this.qfInvoiceNum +'</option>';
$('select#typeList').append(output);
}); 

If I want the value of this.typeName with escape/special characters the provided solution does not work. 如果我想要带有转义/特殊字符的this.typeName的值,则提供的解决方案不起作用。 I can't use data-attributes or the html option value. 我不能使用数据属性或html选项值。

However, after much research, I found a great solution for this problem if you are looking to pass in values to data-attributes that have escape/special characters. 但是,经过大量研究后,如果您希望将值传递给具有转义/特殊字符的数据属性,我找到了一个很好的解决方案。 Please my answer below. 请在下面给我答案。

After much research I found the solution I was looking for. 经过大量研究后,我找到了我正在寻找的解决方案。 If you want to use escape/special characters in html5 data-attributes than a great solution would be to use encodeURI()/decodeURI(): http://www.w3schools.com/jsref/jsref_decodeuri.asp 如果你想在html5数据属性中使用转义/特殊字符,那么使用encodeURI()/ decodeURI()是一个很好的解决方案: http//www.w3schools.com/jsref/jsref_decodeuri.asp

Solution for this question: 这个问题的解决方案:

Encoded url version of the value and set the value for select: 编码url版本的值并设置select的值:

$.each(data, function(i, val) {
     var uri = val.typeName;
     var res = encodeURI(uri);
     var output = '<option id="' + this.qfInvoiceNum + '" class="type" data-name="' + res + '" >' + this.typeName +' | #' + this.qfInvoiceNum +'</option>';
     $('select#typeList').append(output);
    }); 

On select, grab the encoded uri, then decode uri 在选择时,抓取编码的uri,然后解码uri

$('.selectChange2').change(function() {
    uriTypeGrab = $('select option.type:selected').data('name');
    typeGrab = decodeURI(uriTypeGrab);
});

typeGrab is now in the correct format with all of the escape/special characters and ready to pass to JSON.stringify() for the ajax call. 现在,typeGrab的格式正确,包含所有转义/特殊字符,并准备传递给JSON.stringify()以进行ajax调用。

Not pretty sure to have really understood the whole question. 不太确定真正了解整个问题。 Anyway, here is what I point out as perforce causing some issues. 无论如何,这是我指出的导致一些问题的因素。

When you create an <option> like this one: 当你像这样创建一个<option>

'<option class="type" id="' + this.qfInvoiceNum + '" value="' + this.typeName + '">' + this.typeName +'</option>'

then this.typeName is used to populate both value attribute and node content. 然后this.typeName用于填充value属性和节点内容。

In the latter case all is fine, as you reported, since the current typeName content has nothing special here, at the HTML point of view. 在后一种情况下,一切都很好,正如您所报告的那样,因为当前的typeName内容在HTML视点中没有任何特殊之处。

But in the value attribute, the content goes inside of quotes, and here it cannot be neutral as well. 但是在value属性中,内容在引号内,并且在这里它也不能是中性的。

Without more deeper analyzing and testing I can't be strictly affirmative, but a first, quite simply, attempt would be not to populate value attribute at all, using the HTML capability to transparently infer option value from its node content when no value has been given. 如果没有更深入的分析和测试,我不能严格肯定,但首先,非常简单,尝试根本不会填充value属性,使用HTML功能从没有value时从其节点内容透明地推断option值给出。

This way, the content should escape the constraints of the formulation in writing value . 这样,内容应该逃避写作value的制定约束。 Let me know if it worked... 让我知道它是否有效......

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

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