简体   繁体   中英

JQuery don't recognized my (well formed) HTML tag. Possible BOM

Jquery print a syntax error when I try to interpret an AJAX response from my Apache server. The file is printed in php with Smarty as template engine.

Here is a simple minimal working exemple of the "bug" (from the chromium javascript terminal)

> $.post('newtasklist',{name: 'hello'},function(ans){console.log($(ans))})

XHR finished loading: "http://localhost/narasimha/newtasklist". jquery-1.9.1.js:8526

Uncaught Error: Syntax error, unrecognized expression: <li><a><input type="hidden" class="listid" value="4" />hello(0)</a></li> 

As you see, it's not a problem in my HTML tag. I thought it could be a BOM (I work under gedit 3.6.2), so I've bomstrip-files every .php and .tpl files of my project directory... but it hasn't changed anything.

In addition, I've add ans.replace(/^\/, '') to my answer script, but it don't work either.

Does anyone think about another solution?

Edit: I've tried to count the number of bytes in the answer. It tell me "75", 2 bytes more than the actual string. But I don't know the origin of this difference.

> $.post('newtasklist',{name: 'salut'},function(ans){
    console.log('ans : ', ans); 
    console.log('n_ans : ', ans.length);})

ans :  
  <li><a><input type="hidden" class="listid" value="13" />salut(0)</a></li>
n_ans :  75

> '<li><a><input type="hidden" class="listid" value="13" />salut(0)</a></li>'
  .length
73

I never worked with smarty, but i have some thoughts, maybe they can help you to find the problem. It looks more like a smarty/php error to me

  • what happens when you just open the post-url in the browser? does it show the same issue?
  • i don't know how sensitve smarty is... what DOCTYPE do you use... if it's HTML5 then the input-tag has no closing / slash
  • maybe you want to share some of the php that generates the html

I found the problem : the JQuery() function ( $() ) don't like being feed with a string beginning with a newline. A simple trim() -like regex .replace() have solved the problem. Thanks everyone for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM