What is the error of this Uncaught SyntaxError: Unexpected token <when ​​using eval in jquery?

I have a simple ajax call:

 function message(){ $.ajax({ type: "GET", url: "/file/timestamp="+ timestamp, async: true, cache: false, success: function(data){ var json = eval('('+data+')'); console.log(json); } }); } 

and I get the error Uncaught SyntaxError: Unexpected token < on this line: var json = eval('('+data+')');

any ideas?

thanks.

edit: some error details:

 $.ajax.successajax.js:9 f.Callbacks.njquery.js:2 f.Callbacks.o.fireWithjquery.js:2 wjquery.js:4 f.support.ajax.f.ajaxTransport.send.d 

here are some php if helps

 public function fileAction() { $this->getHelper('viewRenderer')->setNoRender(); $filename = '/test/text.txt'; $front = Zend_Controller_Front::getInstance(); $data = $front->getRequest()->getParams(); $lastModif = !empty($data['timestamp']) ? $data['timestamp'] : 0; $currentModif = filemtime($filename); while($currentModif <= $lastModif){ usleep(10000); clearstatcache(); $currentModif = filemtime($filename); } $response = array(); $response['msg'] = file_get_contents($filename); $response['timestamp'] = $currentModif; echo json_encode($response); } 

If I run this action, I get json: {"msg":"message","timestamp":1331599879} , but for some reason the answer is not that, but some html

+4
source share
3 answers

It depends on what is inside the data . You are using eval , so everything in data is executed. Please post data here,

+2
source

What do you expect to return as data? Executing eval will try to execute (data), which does not look like it will be the correct javascript. If you just want to save a line you can do:

 var json = "(" + data + ")"; 
+2
source

jholloman almost had it ...

 var json = eval("(" + data.responseText + ")"); 
0
source

Source: https://habr.com/ru/post/1401081/


All Articles