Unexpected end of JSON input after migrating/upgrading

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -
Number of replies: 18


There isn't any JSON ...

In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Can you tell us when the "Unexpected end of JSON input" appears, for example, when an assignment is being uploaded? Or when a file resource is being uploaded? Or when a course image is being uploaded?

Maybe it happens for all of these but it would help if you could give examples of specific uploads that get this error.

In reply to Leon Stringer

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -
Hello Leon,
The error appears when I'm uploading a recovery file to restore a course. what I don't understand is that the first 3 courses that I restored no error appeared but in the 4th this happened.

the files are in a repository folder.

I'm uploading some more captures that I took at the last try hopping this would help to clarify the problem
Attachment Captura de pantalla 2021-01-25 213910.jpg
Attachment Captura de pantalla 2021-01-25 213943.jpg
In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -

more captures

Attachment Captura de pantalla 2021-01-25 214055.jpg
Attachment Captura de pantalla 2021-01-25 214207.jpg
In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -

the last ones

Attachment Captura de pantalla 2021-01-25 214307.jpg
Attachment Captura de pantalla 2021-01-25 214357.jpg
In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Have you tried this from another computer? Looking at the captures there are requests to Kaspersky Labs that respond with 503 Service Unavailable. So I'm wondering if there's some Kaspersky Labs software on your computer that could be blocking this in your browser?

In reply to Leon Stringer

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -
I tried from other computer and its the same error but without the karspersky problem.




<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>504 Gateway Timeout</title>

</head><body>

<h1>Gateway Timeout</h1>

<p>The gateway did not receive a timely response

from the upstream server or application.</p>

<p>Additionally, a 504 Gateway Timeout

error was encountered while trying to use an ErrorDocument to handle the request.</p>

</body></html>


YUI.add("io-base",function(e,t){function o(t){var n=this;n._uid="io:"+s++,n._init(t),e.io._map[n._uid]=n}var n=["start","complete","end","success","failure","progress"],r=["status","statusText","responseText","responseXML"],i=e.config.win,s=0;o.prototype={_id:0,_headers:{"X-Requested-With":"XMLHttpRequest"},_timeout:{},_init:function(t){var r=this,i,s;r.cfg=t||{},e.augment(r,e.EventTarget);for(i=0,s=n.length;i<s;++i)r.publish("io:"+n[i],e.merge({broadcast:1},t)),r.publish("io-trn:"+n[i],t)},_create:function(t,n){var r=this,s={id:e.Lang.isNumberNo?n:r._id++,uid:r._uid},o=t.xdr?t.xdr.use:null,u=t.form&&t.form.upload?"iframe":null,a;return o==="native"&&(o=e.UA.ie&&!l?"xdr":null,r.setHeader("X-Requested-With")),a=o||u,s=a?e.merge(e.IO.customTransport(a),s):e.merge(e.IO.defaultTransport(),s),s.notify&&(t.notify=function(e,t,n){r.notify(e,t,n)}),a||i&&i.FormData&&t.data instanceof i.FormData&&(s.c.upload.onprogress=function(e){r.progress(s,e,t)},s.c.onload=function(e){r.load(s,e,t)},s.c.onerror=function(e){r.error(s,e,t)},s.upload=!0),s},_destroy:function(t){i&&!t.notify&&!t.xdr&&(u&&!t.upload?t.c.onreadystatechange=null:t.upload?(t.c.upload.onprogress=null,t.c.onload=null,t.c.onerror=null):e.UA.ie&&!t.e&&t.c.abort()),t=t.c=null},_evt:function(t,r,i){var s=this,o,u=i.arguments,a=s.cfg.emitFacade,f="io:"+t,l="io-trn:"+t;this.detach(l),r.e&&(r.c={status:0,statusText:r.e}),o=[a?{id:r.id,data:r.c,cfg:i,arguments:u}:r.id],a||(t===n[0]||t===n[2]?u&&o.push(u)sadr.evt?o.push(r.evt):o.push(r.c),u&&o.push(u))),o.unshift(f),s.fire.apply(s,o),i.on&&(o[0]=l,s.once(l,i.on[t],i.context||e),s.fire.apply(s,o))},start:function(e,t){this._evt(n[0],e,t)},complete:function(e,t){this._evt(n[1],e,t)},end:function(e,t){this._evt(n[2],e,t),this._destroy(e)},success:function(e,t){this._evt(n[3],e,t),this.end(e,t)},failure:function(e,t){this._evt(n[4],e,t),this.end(e,t)},progress:function(e,t,r){e.evt=t,this._evt(n[5],e,r)},load:function(e,t,r){e.evt=t.target,this._evt(n[1],e,r)},error:function(e,t,r){e.evt=t,this._evt(n[4],e,r)},_retry:function(e,t,n){return this._destroy(e),n.xdr.use="flash",this.send(t,n,e.id)},_concat:function(e,t){return e+=(e.indexOf("?")===-1?"?":"&")+t,e},setHeader:function(e,t){t?this._headers[e]=t:delete this._headers[e]},_setHeaders:function(t,n){n=e.merge(this._headers,n),e.Object.each(n,function(e,r){e!=="disable"&&t.setRequestHeader(r,n[r])})},_startTimeout:function(e,t){var n=this;n._timeout[e.id]=setTimeout(function(){n._abort(e,"timeout")},t)},_clearTimeout:function(e){clearTimeout(this._timeout[e]),delete this._timeout[e]},_result:function(e,t){var n;try{n=e.c.status}catch(r){n=0}n>=200&&n<300||n===304||n===1223?this.success(e,t):this.failure(e,t)},_rS:function(e,t){var n=this;e.c.readyState===4&&(t.timeout&&n._clearTimeout(e.id),setTimeout(function(){n.complete(e,t),n._result(e,t)},0))},_abort:function(e,t){e&&e.c&&(e.e=t,e.c.abort())},send:function(t,n,i){var s,o,u,a,f,c,h=this,p=t,d={};n=n?e.ObjectNo:{},s=h._create(n,i),o=n.method?n.method.toUpperCase():"GET",f=n.sync,c=n.data,e.Lang.isObject(c)&&!c.nodeType&&!s.upload&&e.QueryString&&e.QueryString.stringify&&(n.data=c=e.QueryString.stringify(c));if(n.form){if(n.form.upload)return h.upload(s,t,n);c=h._serialize(n.form,c)}c||(c="");if(c)switch(o){case"GET":case"HEAD":case"DELETE":p=h._concat(p,c),c="";break;case"POST":case"PUT":n.headers=e.merge({"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},n.headers)}if(s.xdr)return h.xdr(p,s,n);if(s.notify)return s.c.send(s,t,n);!f&&!s.upload&&(s.c.onreadystatechange=function(){h._rS(s,n)});try{s.c.open(o,p,!f,n.username||null,n.password||null),h._setHeaders(s.c,n.headers||{}),h.start(s,n),n.xdr&&n.xdr.credentials&&l&&(s.c.withCredentials=!0),s.c.send(c);if(f){for(u=0,a=r.length;u<a;++u)d[r[u]]=s.c[r[u]];return d.getAllResponseHeaders=function(){return s.c.getAllResponseHeaders()},d.getResponseHeader=function(e){return s.c.getResponseHeader(e)},h.complete(s,n),h._result(s,n),d}}catch(v){if(s.xdr)return h._retry(s,t,n);h.complete(s,n),h._result(s,n)}return n.timeout&&h._startTimeout(s,n.timeout),{id:s.id,abort:function(){return s.c?h._abort(s,"abort"):!1},isInProgress:function(){return s.c?s.c.readyState%4:!1},io:h}}},e.io=function(t,n){var r=e.io._map["io:0"]||new o;return r.send.apply(r,[t,n])},e.io.header=function(t,n){var r=e.io._map["io:0"]||new o;r.setHeader(t,n)},e.IO=o,e.io._map={};var u=i&&i.XMLHttpRequest,a=i&&i.XDomainRequest,f=i&&i.ActiveXObject,l=u&&"withCredentials"in new XMLHttpRequest;e.mix(e.IO,{_default:"xhr",defaultTransport:function(t){if(!t){var n={c:e.IO.transports[e.IO._default](),notify:e.IO._default==="xhr"?!1:!0};return n}e.IO._default=t},transports:{xhr:function(){return u?new XMLHttpRequest:f?new ActiveXObject("Microsoft.XMLHTTP"):null},xdr:function(){return a?new XDomainRequest:null},iframe:function(){return{}},flash:null,nodejs:null},customTransport:function(t){var n={c:e.IO.transports[t]()};return n[t==="xdr"||t==="flash"?"xdr":"notify"]=!0,n}}),e.mix(e.IO.prototype,{notify:function(e,t,n){var r=this;switch(e){case"timeout":case"abort":case"transport error":t.c={status:0,statusText:e},e="failure";default:r[e].apply(r,[t,n])}}})},"3.17.2",{requires:["event-custom-base","querystring-stringify-simple"]});


In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Matt T -
What web server are you using? Apache or nginx? Or are you using shared hosting?

You probably need to increase the maximum file upload size in your web server software and/or PHP. See below link, which focuses mainly on Apache and PHP.

In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

I can see two things in your latest screenshot 1) the backup file appears to be 715.7 MB and 2) the XHR request is getting a 504 Gateway Timeout after 5 minutes. So maybe the the problem is that because the course backup is large a timeout is being exceeded.

Check PHP's max_execution_time, shown in PHP info. This may be 300 (i.e. 5 minutes). You could try doubling it.

In PHP info also check Server API, is it "FPM/FastCGI"? If so, then the next thing to check is Apache's TimeOut setting and try doubling that. This will be set in a .conf file somewhere, you may need to use grep if there are many files to search.

When changing any settings you probably need to reload or restart the corresponding service (e.g. systemctl restart httpd or systemctl restart php-fpm).

If your server is managed for you then you may need to contact the hosting provider for assistance with this.

If I'm correct about the size of the course backup you may want to try using Moosh to perform the restore at the command line as this avoids some issues with timeouts.

In reply to Leon Stringer

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -
I already changed the max_execution_time and the same error happens.

these are the actual settings for php.ini:

display_errors = On
max_execution_time = 10000
max_input_time = 10000
max_input_vars = 10000
memory_limit = 900M
post_max_size = 900M
session.gc_maxlifetime = 10000
session.save_path = "/var/cpanel/php/sessions/ea-php70"
upload_max_filesize = 900M
zlib.output_compression = On


I can't find server api ... or where to change apache's timeout settings
In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Server API is shown in Site administration ▸ Server ▸ PHP info?:

Cropped screenshot of PHP Info with "Server API: FPM/FastCGI" highlighted.

If your site is hosted on a managed service that you use a web interface to manage (e.g. cPanel) then you may need to contact the hosting company to find out how to change Apache's TimeOut (they may not allow this).

To explain, if Apache is using Server API: FPM/FastCGI then when Apache runs some PHP it passes it to the FPM process. FPM runs the PHP then sends the results to Apache. But if the PHP takes a long time (for example when restoring a large course backup) then Apache may give up waiting and show the 503 Service Unavailable error instead. TimeOut controls how long Apache waits for FPM to run the PHP.

If Server API is not FPM/FastCGI (e.g. if it's Apache 2.0 Handler) then the problem may be something else and changing the Apache TimeOut may not help.

Average of ratings: Useful (1)
In reply to Leon Stringer

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -
Thank you so much for your help!
I contacted my hosting service and I'm currently waiting for an answer.

What I could recollect from the debugging message is this:

Syntax Error
Unexpected token < in JSON at position 0

at JSON.parse ()
at Object.Y.namespace.parse https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:24483:18)
at FilePickerHelper.complete (https://stm-learning.com.ar/lib/javascript.php/1612908620/repository/filepicker.js:54:84)
at Y.Subscriber._notify (https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:13680:35)
at Y.Subscriber.notify (https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:13709:24)
at Y.CustomEvent._notify (https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:13372:17)
at Y.CustomEvent._proSubs (https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:13498:36)
at Y.CustomEvent.fireSimple (https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:13465:18)
at Y.CustomEvent._fire (https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:13447:29)
at IO.fire (https://stm-learning.com.ar/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple.js:14600:22)


I'm not sure if this is going to help, but I'm desperate to fix this problem!!
In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

I asked about PHP's Server API value, once and again, but you won't tell us.

As far as I'm aware the problem is: during an attempted restore of a large course backup (715.7 MB) the web server (not Moodle) is returning an error: 504 Gateway Timeout. We don't know why but it may be because the size of the course backup means that something is taking longer than is allowed (some timeout setting is exceeded).

It may be that your Moodle site needs a server with a better specification.

If you're desperate to fix this maybe you need to consider some commercial support options.

In reply to Leon Stringer

Re: Unexpected end of JSON input after migrating/upgrading

by Josefina Osán -
I understand, Thank you for you support and time!!
In reply to Josefina Osán

Re: Unexpected end of JSON input after migrating/upgrading

by Anze Pratnemer -
Try to update to latest Moodle 3.10, last week similar problem was fixed..
In reply to Anze Pratnemer

Re: Unexpected end of JSON input after migrating/upgrading

by Mark Rosthorn -

I have experienced 'Unexpected token < in JSON at position 0 syntax error message when clicking on 'Enrol users' button in a course.

Happens when visiting server externally but not when visiting local ip/localhost.

I un-ticked caching javascript and it now works okay. 

Any advice whether a firewall would be causing this would be appreciated and maybe a solution.

platform: Apache mysql (8.0.23-0ubuntu0.20.04.1) PHP 7.4 all up to date and new install of 3.10.2 with no plugins added yet.

In reply to Mark Rosthorn

Re: Unexpected end of JSON input after migrating/upgrading

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Happens when visiting server externally but not when visiting local ip/localhost – do you mean when accessing Moodle with different URLs? Or that internally with the same URL but using a different IP address?

Can you tell if this is with the same /lib/ajax/service.php?sesskey=…&info=core_get_fragment request as the OP reported? You can use your browser's developer tools (for example Network Monitor in Firefox) to look for this URL and see what the response body is. It should start:

[{"error":false,"data":{"html":"...

Assuming the site is HTTPS, is the certificate installed on the server, or on a device in front of the server? I noticed on my site the Content-Type header is text/html on my test site, whereas it surely should be application/json. I wondered if something might object to that (I'm thinking out loud – I've got not idea what would do that).

In reply to Leon Stringer

Re: Unexpected end of JSON input after migrating/upgrading

by Mark Rosthorn -
The certificate is loaded on a device in front of server. When I switch off caching of javascript in Moodle config I can get external access to work but not when visiting internally.
Tried adding.. into sites-available/enabled area of apache2 and loaded it.
#Initialize mod_rewrite
RewriteEngine On FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 12 Jan 1980 05:00:00 GMT"
this does same only works externally not internally on correct domain.
Sorry don't get the network reporting bit of your message - I use chrome dev tools and just get the error popping up but no hint as to what it doesn't find. Although the error is saying that json return is starting with a '<' so html being returned when it expects a json starter.
Attachment error_LI.jpg
In reply to Mark Rosthorn

Re: Unexpected end of JSON input after migrating/upgrading

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

The main request made when Enrol users is clicked is /lib/ajax/service.php?sesskey=…&info=core_get_fragment. Hopefully you can find this in the list of requests:

Cropped screenshot of Chrome developer tools with request "service.php?sesskey=...&info=core_get_fragment" highlighted

If you click on it in your browser's developer tools you should be able to see the response and whether it contains something not JSON, possibly HTML as you say. It may even contains information that helps solve this. If the response to that request looks okay, you'll have to check any other requests (there may only be this one).

If the certificate is in front of the server and internal connections are bypassing this then I had wondered if something could be inspecting and changing the contents mid-stream. That said Moodle should only have one URL and that would be HTTPS (ideally) or HTTP but not both, and HTTPS would prevent modification of the stream (unless there was some MITM involved).

I can't think why switching Cache Javascript off would affect this. The only change I can see is that a JavaScript link in the returned fragment (lib/form/form.js) is set to use a non-cached copy.