LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2017, 01:16 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
how to fix 'unterminated string literal' error


I am trying to write a jsfiddle program and need to NOT have my AJAX call in the script so:

I copied the firebug response from my script that used an AJAX call with type 'json' as the returned data . I'm assuming this value is a json object.

I want to assign this response (shown in bold below) to a var (data) and pass it to a function as below:

I am getting syntax error: unterminated string literal.

How can I fix this.

var data={"0":"\/home\/rick\/Desktop\/testFolder","2":"\/home\/rick\/Desktop\/testFolder\/Pictures","3":"\/home
\/rick\/Desktop\/testFolder\/Pictures\/folder1","5":"\/home\/rick\/Desktop\/testFolder\/folder1","6"
:"\/home\/rick\/Desktop\/testFolder\/folder1\/anotherFolder","4":"\/home\/rick\/Desktop\/testFolder\
/folder2","1":"\/home\/rick\/Desktop\/testFolder\/folder3"}


I have tried adding single quotes and concatenation to solve this but that doesn't work, at least the way I'm trying to do it.

Code:
var data='{"0":"/home/rick/Desktop/testFolder","2":"/home/rick/Desktop/testFolder/Pictures","3":"/home';
data+='/rick/Desktop/testFolder/Pictures/folder1","5":"/home/rick/Desktop/testFolder/folder1","6"';
data+=':"/home/rick/Desktop/testFolder/folder1/anotherFolder","4":"/home/rick/Desktop/testFolder';
data+='folder2","1":"/home/rick/Desktop/testFolder/folder3"}';



showDirs(data); // substitute for request.done(showDirs); in AJAX


// ========================== FUNCTION showDirs() ==================

function showDirs(data) { .........
 
Old 07-05-2017, 02:24 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
This is basically a JSON response-string, and what I think that I'd do is to create the corresponding array, however you want to do that, then ask JavaScript to JSON-encode it for you.
 
Old 07-05-2017, 03:16 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks for the reply 'sundialsvcs',

How would you do that? Everything I try when assigning this to a var or function gives me the error.


original response:

{"0":"\/home\/rick\/Desktop\/testFolder","2":"\/home\/rick\/Desktop\/testFolder\/Pictures","3":"\/home
\/rick\/Desktop\/testFolder\/Pictures\/folder1","5":"\/home\/rick\/Desktop\/testFolder\/folder1","6"
:"\/home\/rick\/Desktop\/testFolder\/folder1\/anotherFolder","4":"\/home\/rick\/Desktop\/testFolder\
/folder2","1":"\/home\/rick\/Desktop\/testFolder\/folder3"}

response without backslashes assigned to data:

var data={"0":"/home/rick/Desktop/testFolder","2":"/home/rick/Desktop/testFolder/Pictures","3":"/home
/rick/Desktop/testFolder/Pictures/folder1","5":"/home/rick/Desktop/testFolder/folder1","6"
:"/home/rick/Desktop/testFolder/folder1/anotherFolder","4":"/home/rick/Desktop/testFolder
folder2","1":"/home/rick/Desktop/testFolder/folder3"};

Shouldn't this assign a javascript object to data???

Code:
var result = [];

for(var i in data) {
    result.push([i, data [i]]);
    alert(result[i]);
 }
result of this code:

SyntaxError: unterminated string literal


estFolder","2":"/home/rick/Desktop/testFolder/Pictures","3":"/home
 
Old 07-05-2017, 03:25 PM   #4
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
[SOLVED]

I GOT IT!!!!!!!!!!!

var data={"0":"/home/rick/Desktop/testFolder",
"2":"/home/rick/Desktop/testFolder/Pictures",
"3":"/home/rick/Desktop/testFolder/Pictures/folder1",
"5":"/home/rick/Desktop/testFolder/folder1",
"6":"/home/rick/Desktop/testFolder/folder1/anotherFolder",
"4":"/home/rick/Desktop/testFolderfolder2",
"1":"/home/rick/Desktop/testFolder/folder3"};

Thanks again 'sundialsvcs',

R
 
Old 07-05-2017, 03:40 PM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Glad you got it worked out. Can you please mark the thread as "Solved" (see "Thread Tools" at the top of this thread).

Also, note the missing slash after "testFolder" in the "4" variable.
 
Old 07-05-2017, 08:03 PM   #6
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks hydrurga,

I clicked on Threads solved but don't see anything here immediately.

R
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Lubuntu 15.04 "error: invalid string constant "thunar-statusbar", expected valid string constant" error knobby67 Ubuntu 1 12-09-2016 12:19 AM
SED command failing while substituting variable with error unterminated `s' command vnarvankar Linux - Software 2 03-10-2016 09:10 AM
[SOLVED] Replacing \ with "" gives ^ unterminated string threezerous Linux - Newbie 4 09-17-2014 04:29 AM
Problem using sed with variable created using xargs - unterminated error wrankin Programming 5 11-14-2011 07:53 PM
Unable to ftp error : unknown host or invalid literal address Voyager7 Linux - Newbie 2 01-18-2011 08:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration