开发者

Having issues with node.save service between drupal and appcelerator titanium

开发者 https://www.devze.com 2023-02-07 18:36 出处:网络
I am writing my first app using Appcelerator Titanium and I\'ve hit a snag that I can seem to shake. Every other service I have used is working through the JSON server (node.get, view.get,system.conne

I am writing my first app using Appcelerator Titanium and I've hit a snag that I can seem to shake. Every other service I have used is working through the JSON server (node.get, view.get,system.connect) but I cannot for the life of me get a working solution of node.save. I've tried searching for people in my same boat and can't really find anything but I also cannot find a working solution anywhere.

I used the following blog post as a starting point: http://civicactions.com/blog/2010/may/02/tutorial_code_developing_apps_iphoneipadandroid_using_drupal_base_system

I've tried both JSON and XMLRPC but I get no response with JSON and Access Denied with XMLRPC. If I plug my JSON into the services page through drupal admin it will create a node (not a CCK node but it worked with story) but going through the app I get nothing.

The following is my output trying with XMLRPC:

Node object -

[INFO] {
sessid = b03429453c85d4bf3d600dff6511f70f;
title = "This is a new node.";
type = story;
}



[INFO] xmlrpc: begin
[INFO] xmlrpc: url: http://mysite/services/xmlrpc
[INFO] xmlrpc: method: node.save
[INFO] xmlrpc: p: story
[INFO] xmlrpc: p: This is a new node.
[INFO] xmlrpc: p: b03429453c85d4bf3d600dff6511f70f

XML being sent -

[INFO] xmlrpc: xml: <methodCall><methodName>node.save</methodName><params><param><string>story</string></param><param><string>This is a new node.</string></param><param><string>b03429453c85d4bf3d600dff6511f70f</string></param></params></methodCall>
[INFO] xmlrpc: end
开发者_高级运维

Response -

[INFO] Received: <?xml version="1.0"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>401</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Access denied</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>

Here is what I am getting with JSON:

[INFO] {"method":"node.save","type":"story","title":"This is a new node.","sessid":"b03429453c85d4bf3d600dff6511f70f"}
[INFO] node.save response: undefined
[WARN] Exception in event callback. {
line = 90;
message = "Unable to parse JSON string";
name = SyntaxError;
sourceId = 204738256;
sourceURL = "file://localhost/Users/justin/Sites/Apps/appname/Resources/add.js";
}

I'm not getting access denied but it isn't sending a response back to the app.

Has anyone else ran into this issue and if so have you been able to find a fix for it?


There are a few problems with the modified JSON server from Sumit's blog at this moment. The patch was made to work with a previous version of services 2. Two days ago I was dealing with the same issue. I was working quite frantically and unfortunaly don't remember anymore how everything unfolded. One of the issues is that the outdated json server module makes services crash. Again, I don't remember the details anymore, but here is the solution I found. It's php 5.2 + only , as it uses json_encode and json_decode. First pull the latest stable version of JSON Server. Main point is that json_decode should return associative arrays instead of php objects, as that is what Drupal is expecting. So you call json_decode($json_string,TRUE), using the boolean switch makes json_decode return assoc arrays. So below a quick and very dirty solution:

     function json_server_server() {
    $_POST = json_decode($_POST['data'],true);
    $_POST = (array)$_POST;
    if (!isset($_POST)) {
      return drupal_to_js(array('error' => TRUE, 'data' => "JSON server accepts POST     requests only."));
    }

  $methods = services_get_all();
  services_strip_hashes($methods);
  $request = $_POST['method'];
  $args = array();


  foreach ($methods as $method) {
    if ($method['method'] == $request) {
      unset($_POST['q']);
      unset($_POST['method']);
      $args = array();
      foreach($method['args'] as $arg) {
        if(isset($_POST[$arg['name']])) {
          $args[] = $_POST[$arg['name']];
        }
        elseif($arg['optional'] == 0) {
          return drupal_to_js(array("error" => TRUE, "data" => "Argument ". $arg['name']   ." not recieved"));
        }
        else {
          $args[$arg['name']] = NULL;
        }
      }
      $result = services_method_call($method['method'], $args);

      if (is_array($result) && $result['error'] === TRUE) return  drupal_to_js(array('error' => TRUE, 'data' => $result['message']));

      return(json_encode($result)); //json encode the result, not including the error
    }
  }

  return drupal_to_js(array('error' => TRUE, 'data' => "Invalid  method $request"));
}


Try wrapping your parameters between quotes; like "node.save"; it worked for me.


node.get, view.get and system.connect use different permissions than node.save. Its likely that they are all authorized for the anonymous user while node.save isn't. Since you mention system.connect, I guess you already try to start an authenticated session before calling node.save. Are you sure the session is properly maintained between calls?

Also,

Exception in event callback. {
  line = 90;
  message = "Unable to parse JSON string";
  name = SyntaxError;
  sourceId = 204738256;
  sourceURL = "file://localhost/Users/justin/Sites/Apps/appname/Resources/add.js";
}

This looks more like an exception in the application code handling the server response than an error server-side. It is likely that this is caused by the server returning an HTTP 403 error without a JSON body on access denied.

0

精彩评论

暂无评论...
验证码 换一张
取 消