3

I'm using the Services Module to make a request from one website to a drupal website (The services module calls a view).

For this I'm using jQuery:

$.ajax({
    url: 'http://test.url/rest/viewname',
    success: function(){ alert('hi'); }
});

This works FINE !

Now I added a "Contextual Filter" to the View, to show only nodes of a specific user. My question: How can I send the uid with jquery?? I tried a lot of ways:

$.ajax({
        url: 'http://test.url/rest/viewname/555',
        ...
    });

Error 404!

$.ajax({
        url: 'http://test.url/rest/viewname?args[uid]=555',
        ...
    });

Argument ignored!

$.ajax({
        url: 'http://test.url/rest/viewname?arguments[0]=555',
        ...
    });

Argument ignored!

$.ajax({
        url: 'http://test.url/rest/viewname',
        data: { uid: '1' }
        ...
    });

Argument ignored!

... and much more :D

Can somebody helo me?

2

When making a jQuery ajax request to a Drupal Services 3.x module REST API endpoint, define the request type of your ajax request like this.

(function ($) {
  var request = $.ajax({
    "url" : "http://test.url/rest/viewname/555",
    "type" : "GET" 
  });
}(jQuery));

Usually a RETRIEVE request would be done using the request type GET, but for an ACTION, like the taxonomy_term Resource action selectNodes (This method returns all nodes with provided taxonomy id.), provided in Services 3.x, you would set the request type to POST. All RESTful services use POST for ACTIONs. In this case, your jQuery Ajax request would look like this:

(function ($) {
  var request = $.ajax({
    "url" : "http://test.url/rest/taxonomy_term/selectNodes",
    "type" : "POST", 
    "dataType" : "json",
    "data" : { "tid" : 555, "pager" : 0 }
  });
}(jQuery));

Each of the resource operations in a REST API use a specific HTTP Method or request type (e.g., GET, PUT, POST, or DELETE). You need to use the correct one or it wont work. The Drupal Services 3.x module adheres to these strictly.

To learn more about REST API request methods and how they relate to resource operations, take a look at the Using HTTP Methods for RESTful Services on the REST API Tutorial site.

| improve this answer | |
1

thank you for the help!

I figured out the problem: to show only nodes from a specific user, it's not enough to add "Author ID" as a exposed filter! You have to add the "Author" relationship, and then you have access to much more filters: choose "User: The user ID (raw)" and check "Exposed ...". If you then scroll down to the point "More" you can set a "Filter identifier" (eg: uid)

After doing this you can call:

http://test.url/endpoint/path?uid=123

I hope I can help someone!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.