admin管理员组

文章数量:1356946

I know there is also however I think this is probablynot a wordpress specific question:

$.get(
    ajax.url, 
    {
        'action': 'ajax',
    }, 
    function( response, status, xhr ) {
        $('ul.event-items').append(response);
        }
);

I'm calling a regular wordpress function that has no 0 in it or whatsoever.

However after every ajax-request I get a trailing zero appended to my document.

How can I fix this? I tried using die(); in my function however the rest of my script will not get rendered in that case.

Is there an easy JS-fix like subsrting() to remove the trailing 0 from my html response?

Kind regards, Matt

I know there is also http://wordpress.stackexchange. however I think this is probablynot a wordpress specific question:

$.get(
    ajax.url, 
    {
        'action': 'ajax',
    }, 
    function( response, status, xhr ) {
        $('ul.event-items').append(response);
        }
);

I'm calling a regular wordpress function that has no 0 in it or whatsoever.

However after every ajax-request I get a trailing zero appended to my document.

How can I fix this? I tried using die(); in my function however the rest of my script will not get rendered in that case.

Is there an easy JS-fix like subsrting() to remove the trailing 0 from my html response?

Kind regards, Matt

Share Improve this question edited Jan 8, 2016 at 15:33 hindmost 7,1803 gold badges32 silver badges41 bronze badges asked Jan 26, 2014 at 18:14 mattmatt 44.5k107 gold badges268 silver badges402 bronze badges 2
  • can you show the string? also try to find out any unwanted echo statements – Nouphal.M Commented Jan 26, 2014 at 18:21
  • You could use a conditional js substr() and check if the last character is '0' and lop it off it is. I'd wanna know where its ing from though, cause it might turn into something else later on. – dgo Commented Jan 26, 2014 at 18:26
Add a ment  | 

3 Answers 3

Reset to default 10

The documentation uses wp_die() at the end of the php function to prevent the 0 (and presumably other problems too).

"wp_die(); // this is required to terminate immediately and return a proper response"

Link to docs

Try this:

...
$('ul.event-items').append(response.substr(response.length-1, 1) === '0'? response.substr(0, response.length-1) : response);

http://www.w3schools./jsref/jsref_substring.asp

Example

Extract characters from a string:

var str = "Hello world!";
var res = str.substring(1,4);
The result of res will be:

ell

本文标签: javascriptRemove trailing 0 from ajaxresponseStack Overflow