Hey,

I'm using 100% json-server for my new module collect_nodes. Json-server is really great!
So if you need some advanced usecases example (either widget client side or service side), feel free to have a look.

Ok, I have an issue with passing an argument which is a javascript associative array through the json server. It's not well understood by the json server classes, so at the end, Drupal Service replies a "missing argument". I use the trick to make it a coma-separated string, and to convert the string in array on the server-side, but this is not a "reliable solution".

Any idea how to solve this ? By the way, this seems to me like a more generic issue, the kind of issues interoperability norms are written for. So I was wondering what was the parameters norms (defined types) that Drupal Service understand, what are those that json server understand, ...and so on. I worked once with interoperability issues between .net and J2EE services, and we had to write some rules to ensure a full interop, so I'm guessing it's the same here right?

Bye

CommentFileSizeAuthor
#17 json_server_js.patch540 bytesespector
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

eliosh’s picture

Version: 5.x-1.x-dev » 5.x-1.0

I found a solution, modifing js source code.
My patch is this :

  _makeURI: function(data_original) {
    var data = data_original.slice(0);
    output = data.shift();
    for (i in data) {
      output += '['+ data[i] +']';
    }
    return output;
  },

At least, i make a copy of the original array passed, and so i can use it without problem.

Hope it helps.

Striky2’s picture

Thx for the post, I'll try this next week and I'll keep you updated.

andremolnar’s picture

Status: Active » Postponed (maintainer needs more info)

So I was wondering what was the parameters norms (defined types) that Drupal Service understand, what are those that json server understand

Check out the handbook pages for services module. Its also worth checking out some of the screencasts.
Basically each service/method defined via the services API defines the number and type of arguments that it will accept.
See: http://drupal.org/node/118126
And: http://drupal.org/handbook/modules/services for general info

Provided your client application posts the correct arguments the correct way - services module will respond appropriately

As for the json_server.js file. I'm considering removing it along with json_load from the module and putting it into an example module that is packaged with the json_server.module. Really it is only one suggested way of making use of json_server from the client side.

marking this as needs more info for now to leave things open for discussion

greenmachine’s picture

+1 to the patch in #1, at least for the 5.x release. The example (http://drupal.org/node/305819) doesn't work unless this patch is in place, could trip up people trying this module out (as it did me). Works after the patch.

tritao’s picture

I'm using json_server on drupal 6.10, to invoke a views service with javascript.

Passing javascript arguments seems not to be working on version 6 too. I know it's still on development, but I wonder if there's already a workaround...?

Passing one character works fine, trying to pass a number with more than two digits or a string with more than one character, single argument or array, is not working, it's like the view is not receiving the argument ...

abritez’s picture

Just ran into this thread with may help http://drupal.org/node/663744 there is a patch in the comments

jdwfly’s picture

Version: 5.x-1.0 » 6.x-2.x-dev
Assigned: Striky2 » Unassigned
Status: Postponed (maintainer needs more info) » Active

I've spent most of my day trying to figure out what the problem is here to no avail.

Modules Used
Services 2.0
JSON 2.x-dev

I am trying to use the views.get method and it works well except when you need to pass arguments into it. From what I have found it will only accept a single digit as an argument. Basically I have a view that accepts a node nid as an argument and it finds the nodes that are related to it via a node reference. The view works great and outputs as expected. If you go to the services browser and try from there it works fine as well.

This is my JSON to post to the server:
{"sessid":"04504ead97225844053707a74736da36","view_name":"sermon_list_json","args":"3","method":"views.get"}

That works just fine but if I try this:
{"sessid":"04504ead97225844053707a74736da36","view_name":"sermon_list_json","args":"166","method":"views.get"}

I get this JSON returned:
{"#error":false,"#data":[]}

No error is reported, but there is not any data returned though which means that there was no argument present for the view (that is the option I selected for the view argument).

By the way this all happens when using javascript. Not to be outdone and to really figure out whether this is a bug or not I wanted to try it out in Python. I plugged away at a python prompt and found out that there must be something about arrays and JSON that is creating this error.

Assume that I have already made a valid connection and so forth...

server.views.get(sessid,'sermon_list_json', '', 196)

That line will return an empty array.

server.views.get(sessid,'sermon_list_json', '', [196])

This line will give me an array with the listing as expected

Somehow this is supposed to work and it does (at least in Python), but there must be something tricky about the conversion of those arguments and arrays. I'll leave this as a bug because I think something may be broken and then again I may not have well-formed JSON, but I wonder why first JSON would work if it was invalid. Any more information can be produced if needed as I really need to figure this out.

skyredwang’s picture

Status: Active » Closed (works as designed)

#7 server.views.get(sessid,'sermon_list_json', '', [196]) will make it work. isn't this by design?

jdwfly’s picture

Status: Closed (works as designed) » Active

I would prefer to have heyrocker or someone else that has been working on json_server for while take a look at this.

sumitk’s picture

I was able to pass arguments to views using views.get.

My code looks like:

view.args = [];
view.args[0] = 'page';

This view is taking content type as an argument.

jdwfly’s picture

@sumitk
Are you using your patched version of the module?

serguitus’s picture

sumitk:

and what if you want to pass an argument that is the value of some edit field? Some idea?

for example:
var valor;
view.args = [];
view.args[0] = valor;

this does not works for me. Some clue?

KarlShea’s picture

It looks like there are two problems with arguments passing: the first one is the Drupal.toJson method not handling arrays correctly (at all), and the second is in drupal_parse_json needing to pass TRUE to json_decode.

I fixed it on my install by using a jQuery plugin to do the JSON encoding for me (http://code.google.com/p/jquery-json/), and then modified that call in json_server.module.

So to sum up:

json_server.js:

Drupal.toJson = function(v) {
  return $.toJSON(v);
};

json_server.module:

function drupal_parse_json($v) {
  // PHP 5 only
  if (function_exists('json_decode')) {
    return json_decode($v, TRUE);
  }
...

(this is on 2.x-dev)

That made all of my views-related stuff work using services 2.2, where I'm using different types of views arguments. Hopefully this helps!

Jackinloadup’s picture

Subscribing

Solution in #13 worked for me. Thanks

Anonymous’s picture

Just spent some time on this issue as well.

Following javascript code gets called:

			Drupal.service('views.get',
					{ 'view_name' : "reportageindelingeninhoud", 'display_id' : "Default", 'args' : ["<?php print $field->content; ?>"] },
					function(status, data) {
					    if(status == false) {
					      alert("FATAL ERROR!!!!!");
					    }
					    else {
					        $.each(data, function(i,item){
						    	$('#feedContent<?php print $field->content; ?>').append("<p>"+ item.node_title + " " + item.nid+"</p>");
				            });
					    }
					  }
					);

I then tracked the problem through the json_server.module php functions, and the following php block causes a NULL to be returned for the args array.

function drupal_parse_json($v) {
  // PHP 5 only
  if (function_exists('json_decode')) {
    return json_decode($v);
  }

But actually, if you comment it, it will work again....

Anonymous’s picture

Just spent some time on this issue as well.

Following javascript code gets called:

			Drupal.service('views.get',
					{ 'view_name' : "reportageindelingeninhoud", 'display_id' : "Default", 'args' : ["<?php print $field->content; ?>"] },
					function(status, data) {
					    if(status == false) {
					      alert("FATAL ERROR!!!!!");
					    }
					    else {
					        $.each(data, function(i,item){
						    	$('#feedContent<?php print $field->content; ?>').append("<p>"+ item.node_title + " " + item.nid+"</p>");
				            });
					    }
					  }
					);

I then tracked the problem through the json_server.module php functions, and the following php block causes a NULL to be returned for the args array.

function drupal_parse_json($v) {
  // PHP 5 only
  if (function_exists('json_decode')) {
    return json_decode($v);
  }

But actually, if you comment it, it will work again....

espector’s picture

FileSize
540 bytes

Drupal.toJson function is flawed and appends an extra comma to the array.
This breaks json_decode.

I created a patch to fix it, attached.

-Eric W. Spector, PMP
http://www.ericwspector.com