Hi, I am trying to get a message to say the value of the data's user id as in line 95. However, the json message isnt recognizing it as a variable and is just printing that line how it is. I am unfamiliar with the syntax here, could somebody help me out? Thanks.
If you want to include variables in text strings using JavaScript, you will have to delimit the string with backticks. This character: `
`user ${data.user.uid} signed up successfully`
The single quote / apostrophe you're using now does not allow for inclusion of variables.
Related
I am getting data from Cardstream payment gateway android sdk (in native module) to react native but the data is not a valid JSON:
'{ __wafRequestID=2021-06-16T08:02:14Z|0e2314f32f|115.186.169.10|gk0GHP1i4V, action=SALE, addressCheckPref=not known,not checked,matched,not matched,partially matched, amount=14, amountRetained=0, avscv2CheckEnabled=Y, caEnabled=Y, cardCVVMandatory=Y, cardExpiryDate=0322, cardFlags=8323072, cardIssuer=UNKNOWN, cardIssuerCountry=United Kingdom, cardIssuerCountryCode=GBR, cardNumberMask=424242******4242, cardNumberValid=Y, cardScheme=Visa, cardSchemeCode=VC, cardType=Visa Credit, cardTypeCode=VC, cftEnabled=N, countryCode=826, currencyCode=826, currencyExponent=2, customerName=fgg, customerReceiptsRequired=Y, cv2CheckPref=not known,not checked,matched,not matched,partially matched, eReceiptsEnabled=N, merchantAlias=100001, merchantID=100001, merchantID2=100001, paymentMethod=card, postcodeCheckPref=not known,not checked,matched,not matched,partially matched, processMerchantID=100001, requestID=60c9b007225c7, requestMerchantID=100001, responseCode=65566, responseMessage=Disallowed cardnumber, responseStatus=2, riskCheckEnabled=Y, riskCheckPref=not known=continue,not checked=continue,approve=continue,decline=decline1,review=authonly,escalate=authonly, riskProcessorID=41, riskProcessorName=Kount, rtsEnabled=Y, scaExemption=lowvalue,trusted, state=finished, surchargeEnabled=Y, surchargeRequired=Y, threeDSCheckPref=authenticated, threeDSEnabled=N, timestamp=2021-06-16 09:02:16, transactionID=112446674, type=1, vcsResponseCode=0, vcsResponseMessage=Success, xref=21061609KX02TQ16YP35TTD}'
How to convert it to valid JSON so that I can parse it? I have tried few regex replacements but some values also have colons which is messing it up.
If you wanted to try an parse this with regex then this might work:
[{,] (?<key>[a-zA-Z0-9_]+)=(?<value>.*?(?=, [a-zA-Z0-9_]+=|\}$))
https://regex101.com/r/Fk7NvR/1
Just loop through the matches and access the captured groups named key and value respectively. The value for riskCheckPref seems suspect but I have no clue about their parsing rules.
An alternate idea would be to remove the outer curly braces, trim white space, split on , (comma space), and split again on the first =.
I converted response to JSON in android and then passed it to react-native instead of passing response to react-native (which gets passed in this invalid format) and then banging my head to convert it to JSON. My bad. Thanks all
I am facing some issues with escaping of back slash, below is the code snippet I have tried. Issues is how to assign a variable with escaped slash to another variable.
var s = 'domain\\username';
var options = {
user : ''
};
options.user = s;
console.log(s); // Output : domain\username - CORRECT
console.log(options); // Output : { user: 'domain\\username' } - WRONG
Why when I am printing options object both slashes are coming?
I had feeling that I am doing something really/badly wrong here, which may be basics.
Update:
When I am using this object options the value is passing as it is (with double slashes), and I am using this with my SOAP services, and getting 401 error due to invalid user property value.
But when I tried the same with PHP code using same user value its giving proper response, in PHP also we are escaping the value with two slashes.
When you console.log() an object, it is first converted to string using util.inspect(). util.inspect() formats string property values as literals (much like if you were to JSON.stringify(s)) to more easily/accurately display strings (that may contain control characters such as \n). In doing so, it has to escape certain characters in strings so that they are valid Javascript strings, which is why you see the backslash escaped as it is in your code.
The output is correct.
When you set the variable, the escaped backslash is interpreted into a single codepoint.
However, options is an object which, when logged, appears as a JSON blob. The backslash is re-escaped at this point, as this is the only way the backslash can appear validly as a string value within the JSON output.
If you re-read the JSON output from console.log(options) into javascript (using JSON.parse() or similar) and then output the user key, only one backslash will show.
(Following question edit:)
It is possible that for your data to be accepted by the SOAP consuming service, the data needs to be explicitly escaped in-band. In this case, you will need to double-escape it when assigning the value:
var s = 'domain\\\\user'
To definitively determine whether you need to do this or not, I'd suggest you put a proxy between your working PHP app and the SOAP app, and inspect the traffic.
a(href="#" id="pickndroppoints" data-pickuppoints="[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]")
jade compile give systax error Unexpected identifier
Is there a way to do this ? I need to send json and based on it certain event is triggered using js
your attribute value for data-pickuppoints is breaking at.
data-pickuppoints="[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]"
as you have double quotes inside the json data. make it like
data-pickuppoints='[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]'
or escape the double quotes
I would say, as jade parameter are regular javascript, that you only have to use single quotes so the value wont break :
a(href="#" id="pickndroppoints" data-pickuppoints='[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]')
But I am not on my computer so I cant test.
I am writing JavaScript templates for a content management system where users fill out text input fields that are passed to my templates.
My problem is the quotation marks in the input fields are not escaped before they are passed to my template, so I have no way of knowing if they will contain single or double quotes, or even both. Whichever way I try to handle the data my code ends up breaking because the quotes terminate the string declaration. I want to run a function on the data to escape quotes but I can't find a way to get the data into a valid variable first.
Is there any way to safely handle the data in JavaScript without it breaking a string variable declaration?
Edit: I'm posting code example;
CMS Text Input Field value is: Who'll win our "Big Contest"?
Text Input Field placeholder macro is [%TextInput%]
I'm building an HTML template for this input, using just JS/HTML/CSS
<script>
(function(){
var textInputStr = "[%TextInput%]";
})();
</script>
This will break the string declaration if the value of TextInput contains a single quote, and vice versa.
This is an awesome question, and one that deserves an answer. Strings in JS don't have a custom delimiter, like in most other modern languages, so you can get really stuck. But one solution is to build a script tag with the placeholder inside it, then find the innerHTML of that tag and grab the string back into a variable. eg
<script id="parseMe" type="text/template">
[%TextInput%]
</script>
then use
var yourString = document.getElementById("parseMe").innerHTML
Now you can manipulate the string as you please.
HTH!
I want to run a function on the data to escape quotes but I can't find a way to get the data into a valid variable first.
Well, you will have to make it a valid string literal before you could run JavaScript functions on it. There's no other way (unless you count an ajax request to the template script to get a string representation of it).
The input fields are not escaped before they are passed to my template
Then fix that. There's nothing you can do about it in JavaScript.
I want to add JSON data with the following string value:
json = "d:\xyz\abc";
This value is coming from a database at runtime. When I am going to display it in datatable, JSON formatting error is displayed. My requirement is that the above value will be displayed as it is in the datatable. Please help.
Escape it with another \:
var json = "d:\\xyz\\abc";
You'd better use a JSON library for your programming language. You don't retrieve database values directly with jquery, aren't you?
So, you'd use something like JSON.escape(my_string_from_db), or, in Ruby language I usually do my_string.to_json.
This will automatically escape everything that needs to be escaped.
Change to this:
json = "d:\\xyz\\abc";
See this question for further information
\ is the escape character in JavaScript strings, it gives special meaning to the character following the slash. Like \t is a tab character, \n is a new line. To put a backslash literal you'll need to use \\
The first backslash says the next character is going to be special, the following backslash says "oh, it's just a backslash."