Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of elements from AJAX response

The AJAX response returns a list of <a> elements:

<a href="/1/">One</a>
<a href="/2/">Two</a>
<a href="/3/">Three</a>

How do I select only the first n a elements from the response?

$.ajax({
    url: '/' + page,
    success: function (res) {
        btn.after($('a', $(res)).slice(0,20));
    }
});

That's what I'm currently trying but I'm getting a Uncaught Error: Syntax error, unrecognized expression followed by the whole response.

like image 952
Jürgen Paul Avatar asked Dec 05 '13 15:12

Jürgen Paul


People also ask

Is there a limit to Ajax calls?

By default, the JavaScript Agent limits the Ajax requests (using XHR or the Fetch API) sent for base or virtual pages. The limit is 250 requests for single-page applications (SPAs) and 50 for non-SPAs.

How many arguments does our Ajax function call require?

The function accepts two arguments: The raw data returned from the server and the 'dataType' parameter. The type of data that you're expecting back from the server.

How do you fire Ajax request on regular interval?

1. Using setInterval() It repeatedly calls the function on the given interval for stop you need to clear the interval using clearInterval() or close the window. Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec.

What are required attributes for Ajax call?

The parameters specifies one or more name/value pairs for the AJAX request. The data type expected of the server response. A function to run if the request fails. A Boolean value specifying whether a request is only successful if the response has changed since the last request.


1 Answers

The response had two closing div elements at the end but no two openings, so I had to make one:

$.ajax({
    url: '/' + page,
    success: function (res) {
        btn.after($('a', $($.trim('<div><div>'+res))).slice(0,20));
    }
});
like image 143
Jürgen Paul Avatar answered Oct 18 '22 20:10

Jürgen Paul