Ajax GET request is empty despite correct query string parameter





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Using a simple Ajax GET request to retrieve some data, it successfully checks if($request->ajax()) {} but then fails any validation because there is no data in the Request $request variable. This happens only on the production server, on localhost everything works fine.



The console shows the intended URL https://example.com/employeeInfo?id=1, then error 422 (Unprocessable Entity). Output from error: function(jqxhr, status, exception) { alert('Exception:', exception); } gives an empty alert message.



View



<script>
(function ($) {
$(document).ready(function() {
$(".team-pic").off("click").on("click", function() {
$id = $(this).data('id');

// Get data
$.ajax({
type: 'GET',
url: 'employeeInfo',
data: {'id':$id},
success: function(data){
var obj=$.parseJSON(data);
// Show output...
},

error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});

});
});
}(jQuery));
</script>


Route



Route::get('/employeeInfo', 'EmployeeController@get');


Controller



public function get(Request $request) {

if($request->ajax()) {

$this->validate($request, [
'id' => 'required|integer',
]);

// Id
$employee = Employee::find(request('id'));

// Create output
$data = ...
echo json_encode($data);

}
}









share|improve this question




















  • 1





    FYI, if you're going to use alert, it only takes one string argument. Eg alert(status + ': ' + exception). Personally, I'd use console.error(status, exception)

    – Phil
    Jan 3 at 23:06








  • 1





    I think what you are looking for in your $employee = Employee... line is: $employee = Employee::find($request->input('id')); Also I think in your validate line I think you want $request->all()

    – ajon
    Jan 3 at 23:09













  • What request() function do you mean? if($request->ajax())...? I could leave it out, but the $request variable will still be empty. And thanks for the console.error suggestion. It doesn't provide new information however.

    – Michiel van Nesselrooij
    Jan 3 at 23:11






  • 1





    @Phil turns out the request helper works as he is using it: laravel.com/docs/5.7/helpers#method-request Which also renders the first half of my above comment incorrect.

    – ajon
    Jan 3 at 23:22








  • 1





    @ajon cheers! I don't know Laravel very well and wouldn't have imagined there'd just be global functions available in such a strict OOP framework. The more you learn, eh 🙂

    – Phil
    Jan 3 at 23:25




















0















Using a simple Ajax GET request to retrieve some data, it successfully checks if($request->ajax()) {} but then fails any validation because there is no data in the Request $request variable. This happens only on the production server, on localhost everything works fine.



The console shows the intended URL https://example.com/employeeInfo?id=1, then error 422 (Unprocessable Entity). Output from error: function(jqxhr, status, exception) { alert('Exception:', exception); } gives an empty alert message.



View



<script>
(function ($) {
$(document).ready(function() {
$(".team-pic").off("click").on("click", function() {
$id = $(this).data('id');

// Get data
$.ajax({
type: 'GET',
url: 'employeeInfo',
data: {'id':$id},
success: function(data){
var obj=$.parseJSON(data);
// Show output...
},

error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});

});
});
}(jQuery));
</script>


Route



Route::get('/employeeInfo', 'EmployeeController@get');


Controller



public function get(Request $request) {

if($request->ajax()) {

$this->validate($request, [
'id' => 'required|integer',
]);

// Id
$employee = Employee::find(request('id'));

// Create output
$data = ...
echo json_encode($data);

}
}









share|improve this question




















  • 1





    FYI, if you're going to use alert, it only takes one string argument. Eg alert(status + ': ' + exception). Personally, I'd use console.error(status, exception)

    – Phil
    Jan 3 at 23:06








  • 1





    I think what you are looking for in your $employee = Employee... line is: $employee = Employee::find($request->input('id')); Also I think in your validate line I think you want $request->all()

    – ajon
    Jan 3 at 23:09













  • What request() function do you mean? if($request->ajax())...? I could leave it out, but the $request variable will still be empty. And thanks for the console.error suggestion. It doesn't provide new information however.

    – Michiel van Nesselrooij
    Jan 3 at 23:11






  • 1





    @Phil turns out the request helper works as he is using it: laravel.com/docs/5.7/helpers#method-request Which also renders the first half of my above comment incorrect.

    – ajon
    Jan 3 at 23:22








  • 1





    @ajon cheers! I don't know Laravel very well and wouldn't have imagined there'd just be global functions available in such a strict OOP framework. The more you learn, eh 🙂

    – Phil
    Jan 3 at 23:25
















0












0








0








Using a simple Ajax GET request to retrieve some data, it successfully checks if($request->ajax()) {} but then fails any validation because there is no data in the Request $request variable. This happens only on the production server, on localhost everything works fine.



The console shows the intended URL https://example.com/employeeInfo?id=1, then error 422 (Unprocessable Entity). Output from error: function(jqxhr, status, exception) { alert('Exception:', exception); } gives an empty alert message.



View



<script>
(function ($) {
$(document).ready(function() {
$(".team-pic").off("click").on("click", function() {
$id = $(this).data('id');

// Get data
$.ajax({
type: 'GET',
url: 'employeeInfo',
data: {'id':$id},
success: function(data){
var obj=$.parseJSON(data);
// Show output...
},

error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});

});
});
}(jQuery));
</script>


Route



Route::get('/employeeInfo', 'EmployeeController@get');


Controller



public function get(Request $request) {

if($request->ajax()) {

$this->validate($request, [
'id' => 'required|integer',
]);

// Id
$employee = Employee::find(request('id'));

// Create output
$data = ...
echo json_encode($data);

}
}









share|improve this question
















Using a simple Ajax GET request to retrieve some data, it successfully checks if($request->ajax()) {} but then fails any validation because there is no data in the Request $request variable. This happens only on the production server, on localhost everything works fine.



The console shows the intended URL https://example.com/employeeInfo?id=1, then error 422 (Unprocessable Entity). Output from error: function(jqxhr, status, exception) { alert('Exception:', exception); } gives an empty alert message.



View



<script>
(function ($) {
$(document).ready(function() {
$(".team-pic").off("click").on("click", function() {
$id = $(this).data('id');

// Get data
$.ajax({
type: 'GET',
url: 'employeeInfo',
data: {'id':$id},
success: function(data){
var obj=$.parseJSON(data);
// Show output...
},

error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});

});
});
}(jQuery));
</script>


Route



Route::get('/employeeInfo', 'EmployeeController@get');


Controller



public function get(Request $request) {

if($request->ajax()) {

$this->validate($request, [
'id' => 'required|integer',
]);

// Id
$employee = Employee::find(request('id'));

// Create output
$data = ...
echo json_encode($data);

}
}






php ajax laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 23:02







Michiel van Nesselrooij

















asked Jan 3 at 22:48









Michiel van NesselrooijMichiel van Nesselrooij

5610




5610








  • 1





    FYI, if you're going to use alert, it only takes one string argument. Eg alert(status + ': ' + exception). Personally, I'd use console.error(status, exception)

    – Phil
    Jan 3 at 23:06








  • 1





    I think what you are looking for in your $employee = Employee... line is: $employee = Employee::find($request->input('id')); Also I think in your validate line I think you want $request->all()

    – ajon
    Jan 3 at 23:09













  • What request() function do you mean? if($request->ajax())...? I could leave it out, but the $request variable will still be empty. And thanks for the console.error suggestion. It doesn't provide new information however.

    – Michiel van Nesselrooij
    Jan 3 at 23:11






  • 1





    @Phil turns out the request helper works as he is using it: laravel.com/docs/5.7/helpers#method-request Which also renders the first half of my above comment incorrect.

    – ajon
    Jan 3 at 23:22








  • 1





    @ajon cheers! I don't know Laravel very well and wouldn't have imagined there'd just be global functions available in such a strict OOP framework. The more you learn, eh 🙂

    – Phil
    Jan 3 at 23:25
















  • 1





    FYI, if you're going to use alert, it only takes one string argument. Eg alert(status + ': ' + exception). Personally, I'd use console.error(status, exception)

    – Phil
    Jan 3 at 23:06








  • 1





    I think what you are looking for in your $employee = Employee... line is: $employee = Employee::find($request->input('id')); Also I think in your validate line I think you want $request->all()

    – ajon
    Jan 3 at 23:09













  • What request() function do you mean? if($request->ajax())...? I could leave it out, but the $request variable will still be empty. And thanks for the console.error suggestion. It doesn't provide new information however.

    – Michiel van Nesselrooij
    Jan 3 at 23:11






  • 1





    @Phil turns out the request helper works as he is using it: laravel.com/docs/5.7/helpers#method-request Which also renders the first half of my above comment incorrect.

    – ajon
    Jan 3 at 23:22








  • 1





    @ajon cheers! I don't know Laravel very well and wouldn't have imagined there'd just be global functions available in such a strict OOP framework. The more you learn, eh 🙂

    – Phil
    Jan 3 at 23:25










1




1





FYI, if you're going to use alert, it only takes one string argument. Eg alert(status + ': ' + exception). Personally, I'd use console.error(status, exception)

– Phil
Jan 3 at 23:06







FYI, if you're going to use alert, it only takes one string argument. Eg alert(status + ': ' + exception). Personally, I'd use console.error(status, exception)

– Phil
Jan 3 at 23:06






1




1





I think what you are looking for in your $employee = Employee... line is: $employee = Employee::find($request->input('id')); Also I think in your validate line I think you want $request->all()

– ajon
Jan 3 at 23:09







I think what you are looking for in your $employee = Employee... line is: $employee = Employee::find($request->input('id')); Also I think in your validate line I think you want $request->all()

– ajon
Jan 3 at 23:09















What request() function do you mean? if($request->ajax())...? I could leave it out, but the $request variable will still be empty. And thanks for the console.error suggestion. It doesn't provide new information however.

– Michiel van Nesselrooij
Jan 3 at 23:11





What request() function do you mean? if($request->ajax())...? I could leave it out, but the $request variable will still be empty. And thanks for the console.error suggestion. It doesn't provide new information however.

– Michiel van Nesselrooij
Jan 3 at 23:11




1




1





@Phil turns out the request helper works as he is using it: laravel.com/docs/5.7/helpers#method-request Which also renders the first half of my above comment incorrect.

– ajon
Jan 3 at 23:22







@Phil turns out the request helper works as he is using it: laravel.com/docs/5.7/helpers#method-request Which also renders the first half of my above comment incorrect.

– ajon
Jan 3 at 23:22






1




1





@ajon cheers! I don't know Laravel very well and wouldn't have imagined there'd just be global functions available in such a strict OOP framework. The more you learn, eh 🙂

– Phil
Jan 3 at 23:25







@ajon cheers! I don't know Laravel very well and wouldn't have imagined there'd just be global functions available in such a strict OOP framework. The more you learn, eh 🙂

– Phil
Jan 3 at 23:25














1 Answer
1






active

oldest

votes


















2














If I were you, I would use a RESTful API with route model binding, specifically the explicit binding.



RouteServiceProvider.php



public function boot() 
{
parent::boot();
Route::model('employee', AppEmployee::class);
}


Route



Route::get('api/employees/{employee}', 'EmployeeController@get');


Controller



public function get(Employee $employee)
{
// The id being valid is already done by forcing it to be an Employee
// It is also an ajax call because it is going to the api route
// This will json_encode the employee object.
return $employee;
}





share|improve this answer


























  • Excellent! I didn't use route model binding before, but reading into it, I got your solution to work with some modifications making it even simpler: left RouteServiceProvider for what it was, Route Route::get('employee/{employee}', 'EmployeeController@get');, and the controller as you suggested. I will credit your answer as correct. Thanks!

    – Michiel van Nesselrooij
    Jan 4 at 8:37












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030871%2fajax-get-request-is-empty-despite-correct-query-string-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If I were you, I would use a RESTful API with route model binding, specifically the explicit binding.



RouteServiceProvider.php



public function boot() 
{
parent::boot();
Route::model('employee', AppEmployee::class);
}


Route



Route::get('api/employees/{employee}', 'EmployeeController@get');


Controller



public function get(Employee $employee)
{
// The id being valid is already done by forcing it to be an Employee
// It is also an ajax call because it is going to the api route
// This will json_encode the employee object.
return $employee;
}





share|improve this answer


























  • Excellent! I didn't use route model binding before, but reading into it, I got your solution to work with some modifications making it even simpler: left RouteServiceProvider for what it was, Route Route::get('employee/{employee}', 'EmployeeController@get');, and the controller as you suggested. I will credit your answer as correct. Thanks!

    – Michiel van Nesselrooij
    Jan 4 at 8:37
















2














If I were you, I would use a RESTful API with route model binding, specifically the explicit binding.



RouteServiceProvider.php



public function boot() 
{
parent::boot();
Route::model('employee', AppEmployee::class);
}


Route



Route::get('api/employees/{employee}', 'EmployeeController@get');


Controller



public function get(Employee $employee)
{
// The id being valid is already done by forcing it to be an Employee
// It is also an ajax call because it is going to the api route
// This will json_encode the employee object.
return $employee;
}





share|improve this answer


























  • Excellent! I didn't use route model binding before, but reading into it, I got your solution to work with some modifications making it even simpler: left RouteServiceProvider for what it was, Route Route::get('employee/{employee}', 'EmployeeController@get');, and the controller as you suggested. I will credit your answer as correct. Thanks!

    – Michiel van Nesselrooij
    Jan 4 at 8:37














2












2








2







If I were you, I would use a RESTful API with route model binding, specifically the explicit binding.



RouteServiceProvider.php



public function boot() 
{
parent::boot();
Route::model('employee', AppEmployee::class);
}


Route



Route::get('api/employees/{employee}', 'EmployeeController@get');


Controller



public function get(Employee $employee)
{
// The id being valid is already done by forcing it to be an Employee
// It is also an ajax call because it is going to the api route
// This will json_encode the employee object.
return $employee;
}





share|improve this answer















If I were you, I would use a RESTful API with route model binding, specifically the explicit binding.



RouteServiceProvider.php



public function boot() 
{
parent::boot();
Route::model('employee', AppEmployee::class);
}


Route



Route::get('api/employees/{employee}', 'EmployeeController@get');


Controller



public function get(Employee $employee)
{
// The id being valid is already done by forcing it to be an Employee
// It is also an ajax call because it is going to the api route
// This will json_encode the employee object.
return $employee;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 23:41

























answered Jan 3 at 23:20









ajonajon

5,06743272




5,06743272













  • Excellent! I didn't use route model binding before, but reading into it, I got your solution to work with some modifications making it even simpler: left RouteServiceProvider for what it was, Route Route::get('employee/{employee}', 'EmployeeController@get');, and the controller as you suggested. I will credit your answer as correct. Thanks!

    – Michiel van Nesselrooij
    Jan 4 at 8:37



















  • Excellent! I didn't use route model binding before, but reading into it, I got your solution to work with some modifications making it even simpler: left RouteServiceProvider for what it was, Route Route::get('employee/{employee}', 'EmployeeController@get');, and the controller as you suggested. I will credit your answer as correct. Thanks!

    – Michiel van Nesselrooij
    Jan 4 at 8:37

















Excellent! I didn't use route model binding before, but reading into it, I got your solution to work with some modifications making it even simpler: left RouteServiceProvider for what it was, Route Route::get('employee/{employee}', 'EmployeeController@get');, and the controller as you suggested. I will credit your answer as correct. Thanks!

– Michiel van Nesselrooij
Jan 4 at 8:37





Excellent! I didn't use route model binding before, but reading into it, I got your solution to work with some modifications making it even simpler: left RouteServiceProvider for what it was, Route Route::get('employee/{employee}', 'EmployeeController@get');, and the controller as you suggested. I will credit your answer as correct. Thanks!

– Michiel van Nesselrooij
Jan 4 at 8:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030871%2fajax-get-request-is-empty-despite-correct-query-string-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas