@JeffreyWay Title not displaying here, probably escaping issue.
<? PHP tag in ajax response
I've deployed an application to Digital Ocean using Forge/Envoyer. Locally everything works fine. On the Digital Ocean server every ajax call responds with <?php prepended to the response. e.g. <?php{"data":{"id":6,"
Any clues? I've tried debugging but I'm not getting anything meaningful. The PHP side is using the following code:
protected function respondWithItem($data, $transformer = null, $resourceKey = null)
{
return $this->respondWithArray($this->response->item($data, $transformer, $resourceKey));
}
protected function respondWithArray(array $array, array $headers = [])
{
$array = array_merge($array, ['meta' => $this->meta]);
return response()->json($array, $this->statusCode, $headers);
}
JavaScript Ajax handling is as follows:
(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
// Quickie PubSub
var o = $({});
$.subscribe = function() { o.on.apply(o, arguments) };
$.unsubscribe = function() { o.off.apply(o, arguments) };
$.publish = function() { o.trigger.apply(o, arguments) };
// Async submit a form's input.
var submitLaravelFormRequest = function(e) {
var form = $(this);
var method = form.find('input[name="_method"]').val() || 'POST';
$.ajax({
type: method,
url: form.prop('action'),
data: form.serialize(),
success: function() {
$.publish('ajax.formrequest.success', form);
$.publish('ajax.request.success', form);
}
});
e.preventDefault();
};
// Offer flash notification messages.
// 'data-remote-success-message' => 'Yay. All Done.'
$.subscribe('ajax.request.success', function(e, element) {
var message = $(element).data('remote-success-message');
if (message) {
$('#flash').html('<div class="alert alert-success hidden-print">' + message + '</div>').fadeIn(300).delay(2500).fadeOut(300);
}
})
// Handle success callbacks. To trigger Task.foo(), do:
// 'data-model' => 'Task', 'data-remote-on-success' => 'foo'
$.subscribe('ajax.formrequest.success', function(e, form) {
triggerFormClickCallback.apply(form, [e, $(form).data('remote-on-success')]);
});
// Trigger the registered callback for a click or form submission.
var triggerFormClickCallback = function(e, method) {
var that = $(this);
// What's the name of the parent model/scope/object.
if ( ! (model = that.closest('*[data-model]').data('model'))) {
return;
}
// As long as the object and method exist, trigger it and pass through the form.
if (typeof window[model] == 'object' && typeof window[model][method] == 'function') {
window[model][method](that);
} else {
console.error('Could not call method ' + method + ' on object ' + model);
}
e.preventDefault();
}
// Handle success callbacks. To trigger Task.foo(), do:
// 'data-model' => 'Task', 'data-remote-on-success' => 'foo'
$.subscribe('ajax.request.success', function(e, element) {
triggerClickCallback.apply(element, [e, $(element).data('remote-on-success')]);
});
// Trigger the registered callback for a click or form submission.
var triggerClickCallback = function(e, method) {
var that = $(this);
// As long as the object and method exist, trigger it and pass through the form.
if (typeof window[method] == 'function') {
window[method](that);
} else {
console.error('Could not call method ' + method );
}
e.preventDefault();
}
// Confirm an action before proceeding.
var confirmAction = function(e) {
var input = $(this);
input.prop('disabled', 'disabled');
bootbox.confirm(input.data('confirm'), function(result) {
if (result == true){
e.preventDefault();
return true;
}
});
input.removeAttr('disabled');
return false;
};
// Dom bindings.
$('form[data-remote]').on('submit', submitLaravelFormRequest);
$('input[data-confirm], button[data-confirm]').on('click', confirmAction);
$('*[data-click]').on('click', function(e) {
triggerFormClickCallback.apply(this, [e, $(this).data('click')]);
});
//$('*[data-click-submits-form]').on('change', submitLaravelRequest);
var displayPrintButton = function() {
if (window.print) {
document.write('<form><input type=button name=print value="Print" onClick="window.print()"></form>');
}
}
// Async submit an ajax request.
var submitLaravelRequest = function(e) {
e.preventDefault();
var element = $(this);
var confirm = element.data('confirm');
if (confirm !== undefined){
bootbox.confirm(confirm, function(result) {
if (result == true){
submitLaravelAjaxRequest(e, element);
}
});
} else {
submitLaravelAjaxRequest(e, element);
}
};
var submitLaravelAjaxRequest = function(e, element) {
var url = element.prop('href');
var method = element.data('method') || 'POST';
$.ajax({
type: method,
url: url,
//data: form.serialize(),
success: function() {
//alert('SUCCESS');
$.publish('ajax.request.success', element);
},
error: function onAjaxUpdateError (jqXHR, textStatus, errorThrown) {
//alert(errorThrown); // TODO handle error
$('#waitingModal').modal('hide');
var errorModal = $('#errorModal');
window.cloud9business.ajax.handleAjaxError(errorModal, jqXHR.responseJSON);
}
});
};
$('a[data-remote-click-submits], button[data-remote-click-submits]').on('click', submitLaravelRequest);
})();
RouteServiceProvider.php contains:
public function map(Router $router)
{
$router->group(['namespace' => $this->namespace], function ($router)
{
foreach (File::allFiles(app_path('Http/Routes')) as $partial)
{
require_once($partial->getPathname());
}
});
}
One of the route files loaded by the above method currently has nothing in it except for a <?php tag and NO NEW LINE. The absence of a new line was the problem. Added a comment to the file and all works perfectly. I don't know why this was more of a problem on the production server than locally. The PHP versions are both 5.6.
Please or to participate in this conversation.