如何避免VB转义字符("")无意识转换为Javascript(\")

时间:2016-06-06 22:06:29

标签: javascript asp.net json ajax vb.net

在我正在创建的网站中,根据网站客户端上用户指定的过滤器,来自asp文件的查询。查询将始终包含一些转义双引号(对于SQL规则),因为VB样式转义是""。但是,当我将此查询传递到javascript文件以运行映射Web服务时,转义的双引号将替换为javascript样式的转义引号,即\" 。

当这最终通过ajax函数查询oracle DB并在google map上绘制点时,它会出错,因为javascript样式的转义字符(\")最终会以某种方式变为(\\&#34) ;),显然不会被SQL接受。下面是一个示例测试以及我在调试时看到的内容。

html隐藏输入分配:

queryStringInput.Value = commandstr + wherestr

来自aspx.vb页面的正确查询(我知道这个语法很好,所以这是我想在js文件中显示的文字):

"SELECT cf.METERID as ""Meter ID"", REPID as ""Rep ID"", DIVISION as ""Division"", OPCTR as ""Op Center"",  CITY as ""City"", ADDRESS as ""Address"", cf.PREMISEID as ""PremiseID"",  daysOnPrem.premCount as ""Days Bad On Premise"",  premRecurr.premCount as ""Premise Recurrences"",  RATECODE as ""Ratecode"", METERFORM as ""Meter Form"", METERSTATUS as ""Meter Status"", METERPOINTSTATUS as ""Meter Point Status"",  BILLINGCYCLE As ""Billing Cycle"", FILENAME As ""Filename"", FILEDATETIME As ""File Date/Time"", LATITUDE As ""Latitude"", LONGITUDE As ""Longitude"", FAILURETYPE As ""Fail Type"", CURRENTSTATUS As ""Current Status"" from COMMUNICATION_FAILURES cf  join (Select meterid, PREMISEID, count(PREMISEID) As premCount from COMMUNICATION_FAILURES group by meterid, PREMISEID) daysOnPrem On daysOnPrem.meterid = cf.meterid  join (Select premiseid, count(premiseID) As premCount from (Select meterid,PREMISEID, count(PREMISEID) As premCount from COMMUNICATION_FAILURES group by meterid, PREMISEID) group by PREMISEID) premRecurr On premRecurr.premiseID = cf.premiseid  WHERE DIVISION = 'Western' AND FILEDATETIME >= to_date('06012016', 'mmddyyyy')"

js命令从html隐藏输入变量获取上述查询(通过测试,我发现这需要用双引号):

var queryStringValue = "\"" + document.getElementById('queryStringInput').value + "\"";

实际显示在js文件中代替查询的内容:

\"SELECT cf.METERID as \"Meter ID\", REPID as \"Rep ID\", DIVISION as \"Division\", OPCTR as \"Op Center\",  CITY as \"City\", ADDRESS as \"Address\", cf.PREMISEID as \"PremiseID\",  daysOnPrem.premCount as \"Days Bad On Premise\",  premRecurr.premCount as \"Premise Recurrences\",  RATECODE as \"Ratecode\", METERFORM as \"Meter Form\", METERSTATUS as \"Meter Status\", METERPOINTSTATUS as \"Meter Point Status\",  BILLINGCYCLE As \"Billing Cycle\", FILENAME As \"Filename\", FILEDATETIME As \"File Date/Time\", LATITUDE As \"Latitude\", LONGITUDE As \"Longitude\", FAILURETYPE As \"Fail Type\", CURRENTSTATUS As \"Current Status\" from COMMUNICATION_FAILURES cf  join (Select meterid, PREMISEID, count(PREMISEID) As premCount from COMMUNICATION_FAILURES group by meterid, PREMISEID) daysOnPrem On daysOnPrem.meterid = cf.meterid  join (Select premiseid, count(premiseID) As premCount from (Select meterid,PREMISEID, count(PREMISEID) As premCount from COMMUNICATION_FAILURES group by meterid, PREMISEID) group by PREMISEID) premRecurr On premRecurr.premiseID = cf.premiseid  WHERE DIVISION = 'Western' AND FILEDATETIME >= to_date('06012016', 'mmddyyyy')\"

后续错误消息(您可以在其中看到三反斜杠):

"{\"Message\":\"Invalid object passed in, \\u0027:\\u0027 or \\u0027}\\u0027 expected. (38): {queryString: \\\"SELECT cf.METERID as \\\"Meter ID\\\", REPID as \\\"Rep ID\\\", DIVISION as \\\"Division\\\", OPCTR as \\\"Op Center\\\",  CITY as \\\"City\\\", ADDRESS as \\\"Address\\\", cf.PREMISEID as \\\"PremiseID\\\",  daysOnPrem.premCount as \\\"Days Bad On Premise\\\",  premRecurr.premCount as \\\"Premise Recurrences\\\",  RATECODE as \\\"Ratecode\\\", METERFORM as \\\"Meter Form\\\", METERSTATUS as \\\"Meter Status\\\", METERPOINTSTATUS as \\\"Meter Point Status\\\",  BILLINGCYCLE As \\\"Billing Cycle\\\", FILENAME As \\\"Filename\\\", FILEDATETIME As \\\"File Date/Time\\\", LATITUDE As \\\"Latitude\\\", LONGITUDE As \\\"Longitude\\\", FAILURETYPE As \\\"Fail Type\\\", CURRENTSTATUS As \\\"Current Status\\\" from COMMUNICATION_FAILURES cf  join (Select meterid, PREMISEID, count(PREMISEID) As premCount from COMMUNICATION_FAILURES group by meterid, PREMISEID) daysOnPrem On daysOnPrem.meterid = cf.meterid  join (Select premiseid, count(premiseID) As premCount from (Select meterid,PREMISEID, count(PREMISEID) As premCount from COMMUNICATION_FAILURES group by meterid, PREMISEID) group by PREMISEID) premRecurr On premRecurr.premiseID = cf.premiseid  WHERE DIVISION = \\u0027Western\\u0027 AND FILEDATETIME \\u003e= to_date(\\u002706012016\\u0027, \\u0027mmddyyyy\\u0027)\\\", connectionString: \\\"Provider=OraOLEDB.Oracle.1;Password=luvP1ck3ym0u#s;Persist Security Info=True;User ID=amiop;Data Source=pnhar200\\\"}\",\"StackTrace\":\"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\\r\\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)\",\"ExceptionType\":\"System.ArgumentException\"}"

那么,这里的解决方法是什么来保持VB文件的格式,而不是让javascript文件重新格式化转义字符?

0 个答案:

没有答案