When trying to

Failed Connection

Oops! We couldn't connect to:

http://headless/drupal-7.39

Try Again
Check your device's network settings and try again.

The drupal site is alive and has everything installed from what I can tell I have tried posting to http://headless/drupal-7.39/?q=drupalgap/system/connect.json and everything comes back fine. The services have everything enabled.

I have tried changing to http://localhost/headless/drupal-7.39 and http://127.0.1.1/headless/drupal-7.39

I have looked at every install doc that has been created for this and with no success. I was able to get connected using the v2 of the module, but it is not far enough in development so I don't even know where to start.

CommentFileSizeAuthor
#6 arc-response-2015 Sep 19 20-21-15.txt41.78 KBdobe
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dobe created an issue. See original summary.

tyler.frankenstein’s picture

Status: Active » Postponed (maintainer needs more info)

Are you compiling to a device, using an emulator, or running in web-app (or Ripple) mode?

dobe’s picture

web-app.

dobe’s picture

Sorry another thing to note is:
themes/easystreet3/easystreet3.js
jdrupal-7.x-1.3.min.js:30 POST: http://headless/drupal-7.39/?q=drupalgap/system/connect.json
http://headless/drupal-7.39/?q=drupalgap/system/connect.json Failed to load resource: the server responded with a status of 400 (: Invalid JSON.)
jdrupal-7.x-1.3.min.js:31 POST: http://headless/drupal-7.39/?q=drupalgap/system/connect.json - 400 -
jdrupal-7.x-1.3.min.js:31 ["Invalid JSON."]
jdrupal-7.x-1.3.min.js:31 Date: Sat, 19 Sep 2015 22:18:53 GMT
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
Server: Apache/2.4.16 (Ubuntu)
Connection: close
Content-Type: application/json
Content-Length: 17
Expires: Sun, 19 Nov 1978 05:00:00 GMT

jdrupal-7.x-1.3.min.js:31 ["Invalid JSON."]

that is what console kicks.

tyler.frankenstein’s picture

Hmmm, it unfortunately sounds like your System Connect call is returning some invalid JSON. I would (as an anonymous user) make a POST to ?q=drupalgap/system/connect.json and then copy the JSON that is output into something like jsonlint.com, and see where/when the JSON is broken.

dobe’s picture

I did what you have said (great idea). However jsonlint says the json is valid. I have attached the output. I tried it being logged in as well and the that json was valid.

I am really running out of ideas lol.

tyler.frankenstein’s picture

What localhost URL are you using to launch your app web-app, http://headless/drupal-7.39/mobile-application ?

Or are you going totally headless and the web-app is available at http://headless ?

Either way, make sure your app is running in the same port/domain as the Drupal site.

dobe’s picture

I am going totally headless, app is installed at http://headless/, drupal is installed http://headless/drupal-7.39/, drupal path is fully functional.

Drupal.settings.site_path = 'http://headless/drupal-7.39';
Drupal.settings.endpoint = 'drupalgap';
Drupal.settings.file_public_path = 'sites/default/files';
drupalgap.settings.mode = 'web-app';

Something to mention. Looking through other issues in this queue. I am running php 5.5.29.

I don't see any EOF issues though.

tyler.frankenstein’s picture

In Drupal, if you visit admin/config/services/drupalgap, is the System Connect test successful?

Also, now that you mention PHP 5.5.*, there are quite a few functions in the drupalgap.module (and other files), that have the & specified in function parameters to pass by reference. But I think all of those need to be removed to run properly in PHP 5.5 (I'm not entirely sure on that though).

But if you're System Connect is returning valid JSON, and the DrupalGap System Connect status page has a green light, then you should be good to go.

pierpa’s picture

Hi there,

I'm having the same problem but seem to have found at least the why.
Short description:

I'm using
* drupal 7.41
* php 5.5.9
* drupalgap 7.x-1.14
* services 7.x-3.12+12-dev

Installed a web application (from drupalgap configuration page) in a subfolder of the drupal "root". The "Test Connection" button says "DrupalGap is configured properly!"

But opening the url of the web app, after displaying the drupal logo it shows the message 'Failed connection to undefined - ["Invalid JSON"]'.

Looking for the "Invalid JSON" string, I've found it in the file ServicesParser.inc inside the Services module.
The line is:

class ServicesParserJSON implements ServicesParserInterface {
  public function parse(ServicesContextInterface $context) {
    $data = json_decode($context->getRequestBody(), TRUE);
    if ($data === NULL) {
      return services_error(t('Invalid JSON.'), 400);
    }
    return $data;
  }
}

Now, looking at the point where the request is fired using the chrome debugger, I see (in the file jdrupal.js inside the web app directory) that the line executed is:

else { request.send(null); }

Does it mean that the request body is empty and so the funcion "parse" above return an error?

Sorry for my bad english and my little understanding in web programming: I'm learning!

Hope this helps

tyler.frankenstein’s picture

Status: Postponed (maintainer needs more info) » Fixed

Upgrade to the development snapshot of the Services module for the fix: #2288775: REST server accepts invalid JSON

pierpa’s picture

@tyler: Confirm that upgrading the Services module fixes the problem.

Thanks a lot!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Ddroid_za’s picture

I have a similar problem, having gone through all the documentation and setup guides I am stumped!

I can get my app running on my localhost fine, no connection errors etc. Exact same setup, IE database and setting on the live domain, gives me the failed connection error.

Only difference is the // Site Path (do not use a trailing slash)
Drupal.settings.site_path = 'http://localhost:8888/mysite';
VS
Drupal.settings.site_path = 'https://www.mysite.com';

In chrome console I can see it breaks here: jdrupal-7.0.5.min.js:2 POST https://www.mysite.com/?q=drupalgap/system/connect.json 400 (Bad Request)

Feel like its a noob mistake, thanks in advance!

dobe’s picture

I am guessing this may be a CORS issue (it has been most of my time dealing with connection issues). Try downloading the the chrome plugin to Allow-Control-Allow-Origin: *. Then see if it works.

Basically since your domains are http://localhost:8888 and https://www.mysite.com Chromes implementation of CORS blocks that activity. Postman sets Allow-Control-Allow-Origin: * automatically I believe so that is why if you test that plugin when looking at http://localhost:8888 it should work. Then you can narrow your issue down to CORS and make sure you have correct headers setup for sending and receiving the requests. It is kinda irritating but once you know whats going on a little it will make dealing with it easier.

Something to note if your wanting to use drupal's $cookie_domain you will need to setup CORS modifications to drupal's headers.

I know I piled a lot here and maybe it won't help. This is just my experience when working with angular and drupal. I have about 5 apps built now and that is the main thing to overcome when dealing with separate domains for app vs site.

Ddroid_za’s picture

Thanks for the reply, appreciate it!

I installed the extension, cleared browser and drupal caches... relaunched same issue.

Just a note, I am not trying to communicate between the two domains, but trying to get each running separately. My apps are installed here: http://localhost:8888/mysite/mobile-application/index.html and here https://www.mysite.com/mobile-application/index.html.

Do you think maybe the https has something todo with it?

tyler.frankenstein’s picture

Ddroid_za, if it isn't CORS, then I'd suspect it is a configuration with your web host. As an example, I heard some hosts don't allow PUT calls to their infrastructure at all (not that that necessarily matters for POST in this case). So I'd suspect some hosts may even restrict certain POST calls as well.

Otherwise, please share your Network tab information from your browser's developer tools, i.e. data sent, response codes, response, etc, that'd also help pinpoint why the connection failure.

Ddroid_za’s picture

@ Tyler, thank you so much for your reply!

Console Information:
Most modules load fine,
...
...
..
app/modules/custom/user_profile/user_profile.js
drupalgap-7.0.2.js:474 themes/easystreet3/easystreet3.js

--- here it breaks ---

jdrupal-7.0.5.min.js:2
POST: https://www.mysite.com/?q=drupalgap/system/connect.json

jdrupal-7.0.5.min.js:2
POST https://www.mysite.com/?q=drupalgap/system/connect.json 400 (Bad Request)
-- success @ jdrupal-7.0.5.min.js:2
-- services_get_csrf_token @ jdrupal-7.0.5.min.js:1
-- Drupal.services.call @ jdrupal-7.0.5.min.js:2
-- success @ jdrupal-7.0.5.min.js:1
-- c.onload @ jdrupal-7.0.5.min.js:1

jdrupal-7.0.5.min.js:2
POST: https://www.mysite.com/?q=drupalgap/system/connect.json - 400 - Bad Request

jdrupal-7.0.5.min.js:2


400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.


Apache Server at mysite.com Port 443

jdrupal-7.0.5.min.js:2 Date: Sun, 29 Jan 2017 08:02:44 GMT
Content-Encoding: gzip
X-Content-Type-Options: nosniff
Server: Apache
Vary: Accept-Encoding
Content-Type: text/html; charset=iso-8859-1
Connection: close
Content-Length: 227

Network Tab Information:
General:
Request URL:https://www.mysite.com/?q=drupalgap/system/connect.json
Request Method:POST
Status Code:400 Bad Request
Remote Address:188.40.0.35:443

Response Headers
Connection:close
Content-Encoding:gzip
Content-Length:227
Content-Type:text/html; charset=iso-8859-1
Date:Sun, 29 Jan 2017 08:22:47 GMT
Server:Apache
Vary:Accept-Encoding
X-Content-Type-Options:nosniff

Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Content-type:application/json
Cookie:SSESSab1a8d3cf8bee74f4c891bc155c62646=1tcdL82ZxTI91d2RMY_UeaqyUrvT4wrj7AaQmmuIcaw; iflychat_status=1; drupalchat=; soundnotification=2; username=admin; uid=1; send_current_message=null; last_timestamp=0; send_current_uid2=0; attach_messages_in_queue=0; running=0; DrupalModuleFilter=activeTab%3Dall; has_js=1
Host:www.mysite.com
Origin:https://www.mysite.com
Referer:https://www.mysite.com/mobile-application/index.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
X-CSRF-Token:S1rj7aemyyW74wIRjTG6dR0ek2ywZxh9Jq9ENofq4nk

Query String Parameters
view URL encoded
q:drupalgap/system/connect.json

tyler.frankenstein’s picture

I guess I'd research more into "400 bad requests" in general and contact your web host. Or try creating a custom Service Resource and verifying that POST commands can even work on your web host:

http://docs.drupalgap.org/7/Services/Create_a_Custom_Service

When it comes time to make the call, don't use the JavaScript/DrupalGap code, instead use something like Firefox Poster to test the POST command: http://tylerfrankenstein.com/code/drupal-services-csrf-token-firefox-poster

Ddroid_za’s picture

I can confirm using Firefox Poster and chrome's postman both return valid json on GET and POST...

tyler.frankenstein’s picture

Then I would next suspect an installed contrib module on your site (usually something that utilizes a non UTF-8 character or has corrupt data) is causing the System Connect procedure to return bogus JSON. Without being able to reproduce this, I'm just taking shots in the dark here. Good luck.

Ddroid_za’s picture

yeah its driving me nuts! I even deleted my entire mobile-application folder, reinstalled the sdk on the server, removed all the modules...same result..

Thing is same files, same db on my localhost works 100%...

Ddroid_za’s picture

Hey tyler, still banging my head here... looking at jdrupal-7.0.5.min.js file. This function system_connect(a) doesnt pass ant "data" object like all the other functions in the file. If I add that in postman like:

var settings = {
"async": true,
"crossDomain": true,
"url": "https://www.mysite.com/?q=drupalgap/system/connect.json",
"method": "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": "b312b3f2-623d-6908-0261-61db723cbd19"
},
"processData": false,
"data": "{\"hello\":\"world\"}"
}

$.ajax(settings).done(function (response) {
console.log(response);
});

Then I get valid json back.... Could this be the reason perhaps?

Ddroid_za’s picture

so finally got it to work for my case... This is a server issue in my case as suspected. After looking at the error logs on the server:

Message: JSON parser error: parse error: premature EOF
Message: Access denied with code 400 (phase 2). Match of "eq 0" against "REQBODY_ERROR" required. [file "************"] [line "60"] [id "200002"] [msg "Failed to parse request body."] [data "JSON parser error: parse error: premature EOF\x0a"] [severity "CRITICAL"]

I saw that our sever requires a valid body. So I ended up adding a data attribute to the system_connect(a) function in jdrupal-7.0.5.min.js. This is a bit of a hack, but at least it works for my specific case.

tyler.frankenstein’s picture

@Ddroid_za thank you for the follow up, and I'm glad you were able to resolve the issue. You should be able to use one of the hook_services_* in DrupalGap to add/edit the Service call, that way you don't have to hack the jdrupal.min.js file. Take a look at the DrupalGap API for examples of those hooks: http://api.drupalgap.org/7/ - Particularly http://api.drupalgap.org/7/jDrupal_src_API.js.html#line18

P.S. checkout the src directory in the jDrupal project, and then with Grunt all those src code files will be compiled into a new jdrupal.js and jdrupal.min.js files, that way you can more easily make modifications to the project if you need.

tyler.frankenstein’s picture

Thank you @Ddroid_za, I was having this same issue on a new project today:

Failed to load resource: the server responded with a status of 400 (Bad Request)

With your suggestion in #24, I have added a permanent fix to jDrupal 7 and so it will now work for everyone: https://github.com/signalpoint/jDrupal/issues/78

The fix will be available with the jDrupal 7.0.6 release, or people can download the latest dev snapshot for the immediate fix: https://raw.githubusercontent.com/signalpoint/jDrupal/7.x-1.x/jdrupal.mi...

Thank you again @Ddroid_za for your contribution!

Asogan’s picture

1. "With your suggestion in #24, I have added a permanent fix to jDrupal 7 and so it will now work for everyone: https://github.com/signalpoint/jDrupal/issues/78"

2. "The fix will be available with the jDrupal 7.0.6 release, or people can download the latest dev snapshot for the immediate fix: https://raw.githubusercontent.com/signalpoint/jDrupal/7.x-1.x/jdrupal.mi..."
____________________________________________________________________________________________________
Hi guys. Same issue. Absolute noob here so sincere apologies. How do I actually incorporate the solution?

On 1. the thread leads to a patch to src/services/services.js.......
Is this the same as the services.js found in mobile-application/src/modules/services/ directory? I had a look in the file using Notepad2 and couldn't see any familiar code in the services.js as per the thread, namely: " if (options.ontimeout) { request.ontimeout = options.ontimeout; }
}"

And couldn't find if (typeof options.data !== 'undefined') { .... to delete

On 2. Do I just copy and paste the code in the https://raw.githubusercontent.com/signalpoint/jDrupal/7.x-1.x/jdrupal.mi... link; into the jdrupal-7.0.5.min.js file in the mobile applications directory?

And do I need to do both?

**UPDATE: Ok for noobs like me, just download the https://raw.githubusercontent.com/signalpoint/jDrupal/7.x-1.x/jdrupal.mi... file, rename it to jdrupal-7.0.5.min.js; and replace the original in the mobile-application folder. Ensure you run cron, and clear cache. Working now! (So excited!)

Max_Headroom’s picture

After 2 days of struggling, this finally sorted me out as well.
Instead of renaming the jDrupal.js file, you can change it in index.html.
Guess an update is needed.
Also, clear your browser cache as well.