admin管理员组文章数量:1356413
i've got 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 an error Uncaught SyntaxError: Unexpected token <
at this line: var json = eval('('+data+')');
any ideas?
thanks.
edit: some more details from the error:
$.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 is some php if is helping
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 response is not this but some html
i've got 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 an error Uncaught SyntaxError: Unexpected token <
at this line: var json = eval('('+data+')');
any ideas?
thanks.
edit: some more details from the error:
$.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 is some php if is helping
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 response is not this but some html
- 3 I'm guessing you are getting XML and not JSON. – Joe Commented Mar 13, 2012 at 0:37
-
If you are getting JSON,
eval
is both unnecessary (jQuery parses the JSON for you) and Evil. – nrabinowitz Commented Mar 13, 2012 at 0:39 - 1 console.log(data) shows what? I am guessing an html error page. – epascarello Commented Mar 13, 2012 at 0:42
- yes, bit no error, just html for some reason. – Patrioticcow Commented Mar 13, 2012 at 0:44
- Well figure that out and maybe your json will start appearing. Get Fiddler and see what the request/response is doing. – epascarello Commented Mar 13, 2012 at 0:47
3 Answers
Reset to default 2It depends on what's inside data
. You're running eval
, so whatever's in data
is being run. Please post the data
here,.
What are you expecting to be returned in as data? Running eval will try to execute (data) which doesn't seem like it will be proper javascript. If you just want to store a string you can do:
var json = "(" + data + ")";
jholloman almost had it...
var json = eval("(" + data.responseText + ")");
本文标签:
版权声明:本文标题:javascript - what error is this Uncaught SyntaxError: Unexpected token < when using eval in jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743990479a2572043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论