退出/特殊字符从用户输入到HTML5使用URL连接code数据属性/德code属性、特殊字符、数据、用户

2023-09-10 17:34:07 作者:折耳猫

这是我的计算器上的第一个问题 - 所有你需要专家的帮助。我们的Web应用程序允许用户输入转义/特殊字符。我们正在做一些测试极端情况下。我能够通过AJAX / JSON通过转义字符。

VAR测试= JSON.stringify({   typeName的:$(#的typeName)VAL()   invoiceType:NewTypeArea }); $阿贾克斯({   键入:POST,   网址:后端/ WebService.php   数据: {     newInvoice:测试   },   数据类型:JSON });

&LT;脚本src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

我的后端开发者自己的事情处理转义/特殊字符。当我拨打另一个电话,我得到以下JSON数据。

  [{作为propName:特殊的\\\,的typeName:输入\\\}]
 
WORD怎么打温度

返回这个数据是正确的。为简单起见 - 我只有一个对象,在这个例子 - 我想借此数据,并把它变成一个下拉列表。

  $。每个(数据,功能(我,VAL){
无功输出='&LT;选项类=输入ID =+ this.qfInvoiceNum +'值=+ this.typeName +'&GT; + this.typeName +'&LT; /选项&GT;';
$('#选择TYPELIST)追加(输出)。
 

});

它正确地显示所有的转义/特殊字符。例如:输入\\显示类型'\\

然后,用户选择列表中的项目之一。我试着捕捉的typeName多种方式与数据属性值等下面是一个例子:

  $('.selectChange1')。改变(函数(){
typeGrab = $('选择option.type:选择)。VAL();
pullArea();
});
 

但是,这是问题的原因。如果他们从控制台返回选择列表项目的特殊/转义字符值

 型'
 

不是

 输入\\\
 

所以,当然,当我通过在var typeGrab为JSON / AJAX调用它是错误的。

是否有一个良好的解决方案就如何从下拉获得逃生/特殊字符?或者,也许我会错了$。每次当我通过数据循环?或者,很有可能,我想的东西很简单。任何帮助或洞察力将是巨大的。

感谢你。

附加信息。第一个解决方案确实可行。不过,我没有使用这个一开始由于这样的事实,有几次,其中所显示的文字是不是我想要抓住 - 这就是为什么我一直想把数据* -attributes

例如:

  $。每个(数据,功能(我,VAL){
无功输出='&LT;选择ID =+ this.qfInvoiceNum +'级=类型数据索引=+ this.typeName +'&GT; + this.typeName +'| #+ this.qfInvoiceNum +'&LT; /选项&GT;';
$('#选择TYPELIST)追加(输出)。
});
 

如果我想this.typeName与逃逸值/特殊字符所提供的解决方案是行不通的。我不能使用数据属性或HTML选项值。

然而,大量的研究后,我发现这个问题的一个很好的解决方案,如果你正在寻找传入值的数据属性有逃逸/特殊字符。请在下面我的答案。

解决方案

大量的研究,我发现我一直在寻找的解决方案之后。如果你想使用转义/ HTML5中的数据属性不是一个很好的解决方案的特殊字符是使用EN codeURI()/德codeURI():的 http://www.w3schools.com/jsref/jsref_de$c$curi.asp

解决方案这个问题:

恩codeD URL的版本,并为选择的值:

  $。每个(数据,功能(我,VAL){
     VAR URI = val.typeName;
     VAR水库= EN codeURI(URI);
     无功输出='&LT;选择ID =+ this.qfInvoiceNum +'级=类型数据NAME =+资源+'&GT; + this.typeName +'| #+ this.qfInvoiceNum +'&LT; /选项&GT;';
     $('#选择TYPELIST)追加(输出)。
    });
 

在选择,抓住EN codeD URI,然后去code URI

  $('。selectChange2)改变(函数(){
    uriTypeGrab = $('选择option.type:选择)。数据(名称);
    typeGrab =去codeURI(uriTypeGrab);
});
 

typeGrab现在是正确的格式与所有的逃生/特殊字符,并准备传递给JSON.stringify()的Ajax调用。

This is my first question on stackoverflow - needing help from all you experts. 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.

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

[{"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'\"\

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:

$( '.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.

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? 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.

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. I can't use data-attributes or the html option value.

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

Solution for this question:

Encoded url version of the value and set the value for 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

$('.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.