I have this part of code in Controller, but when i make this action and IF works fine and the error message appears in console log.
But it looks like 'success' has status true. And it shouldn't
Try {
if ($last['date']) {
if ($last['date']->format('d-m-Y') == $par['date']) {
throw new \Exception('error', 500);
}
}
return new JsonResponse([
'success' => true,
'data' => $content,
'message' => 'success'
]);
} catch (\Exception $exception) {
return new JsonResponse([
'success' => false,
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
]);
}
JQuery
$.ajax({
type: "POST",
url: "/app/url",
data: TableData,
dataType: "json",
success: function(response){
console.log(response.message);
$('#message').html('<p class="alert alert-success">' + response.message + '</p>');
},
error: function (response) {
console.log(response.message);
$('#message').html('<p class="alert alert-danger">' + response.message + '</p>');
}
});
Your AJAX code is receiving the response as success every time, regardless of the JSON content, because you're always sending a 200 response (which is success). To tell AJAX to process the response as an error (and go to the error method instead of success method in your AJAX response handler), you need to send an error code in the response, i.e. 400, like this:
return new JsonResponse([
'success' => false,
'code' => 400,
'message' => $exception->getMessage(),
], 400);
So, if you're throwing your custom Exceptions, you need to set the code property for each according to their real HTTP meaning.
Now, the success and error handlers in AJAX have different parameters. In success, the first parameter is data, simply the data returned from the server. On the other hand, in error, the first parameter is an jqXHR object. To access the data in this object, you have a handful of different parameters, but what you need now, since you have JSON, is jqXHR.responseJSON. So, now your error message will be in response.responseJSON.message, not in response.message. See more about this here.
Related
I have the below ajax js. I am trying to make a POST to my controller with the id . In the line starting with let id ... when I log it to console, I can see the id number but the controller doesn't receive it. Also in XHR/payload I see [object Object] which do not know why.
$(document).on('click', '.delete-task', function(event){
// id comes from data-id
let id = $(event.currentTarget).attr('data-id');
$.ajax({
url: ajaxUrl+'/admin/tasks/delete',
type: 'POST',
// what data you passing.
data: {
'id' : id
},
processData: false,
contentType: false,
dataType: 'json',
success: function(data) {
console.log(data);
}
});
});
Route is; $routes->match(['get', 'post'], '/admin/tasks/delete', 'Admin\Task\Task_Controller::task_delete');
For my controller, I have the below. As it is a post, I expect to be able to get the id by using $this->request->getVar('id') but doesn't work. I always get 'success' => '0 'msg' => "No Task To Delete" returned. Any pointers appreciated.
```
public function task_delete(){
$id = $this->request->getVar('id');
if(empty($id)){
$response = [
'success' => 0,
'msg' => "No Task To Delete",
];
echo json_encode($response);
} else {
$task = new TaskModel();
$task->task_delete($id);
$response = [
'success' => 1,
'msg' => "Task Deleted",
];
echo json_encode($response);
}
}```
So I can see id=103 in the payload in the console but for somereason but reaching the controller. Interesting also is when I log log_message('error', $request->getMethod()); it is a critical error as blank. Even log_message('error', $request->isAjax()); is critical error as blank.
I have created an API which my AJAX post send values to it. AJAX does post and my laravel API does process the values. My issues is with the callback returning the value back to my AJAX post. My AJAX doesn't return the results in the success section when I do console log. I would like the results from my api to can use data to make my condition. At the moment, the console log doesn't even return a value. But in my chrome inspector under preview it shows the response from my API but not in the success section.
AJAX
var fname = "Joe";
var lname = "Test";
var processUrl = "api.example.com/z1";
$.ajax({
type: 'POST',
url: processUrl,
data: {"name": fname,"surname": lname},
dataType: 'json',
success: function(res){
console.log(res);
if(res.length >= 1){
$('#display').val(res.name);
}
}
});
PHP
public function checkResults(Request $request){
$name = $request->name." ".$request->surname;
$result = array();
$result['name'] = [$name];
return response()->json($result,201);
}
For first it will be good to return with 200 OK response code (instead of 201).
Note: If you want to just immediately get the answer for your question only, you can see the last part of this answer (usage of "done/fail" construct instead of "success/error").
Additional:
There is many patterns which are used by Client(Frontend)<->API<->Server(Backend) developers.
Approximately all APIs built without any 500 server error codes. But there is exists also many differences between APIs structures.
One of them is to send response like this (this is the only one example of response):
return response()->json([
'success' => true, // true or false
'message' => "Message about success!",
], 200); // 200, 401, 403, 404, 409, etc
The other approach is to always sending 200 OK, but message can be also about error:
return response()->json([
'success' => false, // true or false
'code' => 404,
'message' => "Resource not found!",
], 200);
This kind of methods will written under try{}catch() and will return only 200, but that messages can imitated also as an error (as in example).
The other (appropriate approach for you) is to change your Frontend AJAX functionality like this:
$.ajax({
type: 'POST',
url: processUrl,
data: {
{{--_token: "{{ csrf_token() }}",--}}
name: fname,
surname: lname
},
dataType: 'json'
}).done(function(res) {
console.log(res);
if(res.length >= 1) {
$('#display').val(res.name);
}
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log("Error: " + textStatus);
});
AJAX .done() function replaces method .success() which was deprecated in jQuery 1.8. This is an alternative construct for the success callback function (like before: "success: function(){...}").
AJAX .fail() function replaces method .error() which was deprecated in jQuery 1.8. This is an alternative construct for the complete callback function (like before: "error: function(){...}").
Note: .error() callback is called on HTTP errors, but also if JSON parsing on the response fails. This is what's probably happening if response code is 200/201 but you still are thrown to error callback.
I believe this is happening because you are sending status code 201 (Created), but you need to send status code 200 (OK) to trigger the success callback.
public function checkResults(Request $request){
$name = $request->name." ".$request->surname;
$result = array();
$result['name'] = [$name];
return response()->json($result,200);
}
I couldn't find it specifically in the jQuery docs, but this SO question addresses it.
Due to the asynchronous nature of Ajax calls, do not put them in the normal execution flow of your program. See this post to get more insight.
A quick fix for your problem is to include the ajax call in a function and call that function anytime you want to interact with the server asynchronously.
var fname = "Joe";
var lname = "Test";
var processUrl = "api.example.com/z1";
ajaxCall();
function ajaxCall() {
$.ajax({
type: 'POST',
url: processUrl,
data: {"name": fname,"surname": lname},
dataType: 'json',
success: function(res){
console.log(res);
if(res.length >= 1){
$('#display').val(res.name);
}
},
error: function() {
console.log('error');
}
});
}
In addition, include an error function in the ajax call settings to handle cases where the ajax request fails. See this answer for alternative styles of doing this.
I have been struggling with getting the Stripe card errors to work properly with my AJAX call. Instead of returning some nice JSON, it is returning a 500 error. Of course, I need to be able to display the error for the user to see. I'm not sure if this is Laravel specific so I thought that was worth mentioning. Here is my try/catch block:
public function charge($type, $amount, $isSubscription = false)
{
try {
Charge::create([
'amount' => $amount,
'currency' => 'usd',
'description' => $type,
'customer' => $this->customer['id'],
]);
} catch (\Stripe\Error\Card $e) {
$body = $e->getJsonBody();
dd($body); // This does not work
}
It seems like I never reach the dd() block of code, but instead I get a 500 in the console and when I preview the error, it is in html like the Laravel exceptions. I have tried using echo json_encode() to return the error, but again, if I'm not even getting to the dd() part, then it makes sense I would never reach echo json_encode.
Here is my AJAX call:
$.ajax({
url: url,
data: data,
type: 'POST',
dataType: 'JSON',
success: function (data) {
console.log(data);
_.showSuccess();
_.clearForm();
},
error: function (response) {
_.hideSubmitProcessing();
console.log(response); // This is returning html and a 500 in the console
}
});
Here is a screenshot of the error in the console:
I've been looking at this for too long and def need another set of eyes to help me out.
I know this is an old post, here's the fix if anyones still looking for it:
\Stripe\Exception\CardException
- this is now used as the main exception for any stripe card exception.
} catch (\Stripe\Exception\CardException $e) {
return response()->json([
'status' => 5,
'message' => $e->getMessage()
]);
} catch (Exception $e) {
return response()->json([
'status' => 5,
'message' => $e->getMessage()
]);
}
I'd like to have a code to add or remove from database bookmarks.
The code is ready and it adds and removes from database bookmarks correctly, but when I call the function it keeps returning the json error instead json success even if the code works.
I'd like to know what's wrong with the code (that I got and adapt from somewhere else) because the client side is not receiving the correct values true or false, it only triggers the json beforeSending and json error.
server side:
if($isFavorite) {
// if it's favorite, remove from bookmarks
return json_encode(array("status" => true, "added" => false));
} else {
// if it's not favorite, include into bookmarks
return json_encode(array("status" => false, "added" => true));
}
client side:
<script>
function addItemToUsersList(userId, type, itemId) {
jQuery.ajax({
'url': 'xxx',
'type': 'GET',
'dataType': 'json',
'data': {userid: userId, type: type, itemid: itemId},
'success': function(data) {
console.log('json success');
},
'beforeSend': function() {
console.log('json beforeSending');
},
'error': function(data) {
console.log('json error');
console.log(data.status + ' ' + data.added);
}
});
}
</script>
The console.log(data.status + ' ' + data.added); line logs 200 undefined
How may I return the correct values true or false for both "status" and "added"?
Edit:
The json success is never logged on console, so I don't know what happened on server side. I need to know this because I need to change the class of an element to display an empty or yellow star.
If you are returning the result and not doing anything with that return elsewhere, you will not get any response to your ajax call, so it's undefined. As #MikeC says, you must echo it at some point.
If you are not already echoing it elsewhere, try:
$response = array(
'status' => $isFavourite,
'added' => !$isFavourite
);
echo json_encode($response);
My suggestion is also if 'status' and 'added' are really just the opposite of each other every time, then you probably only need to send 'status' on its own. In your JS you can just check 'status' and reverse the boolean as I've done above, if you want to know what the value of added would be.
var added = !data.status;
Update
If your ajax request is coming back to the error function, the request itself is probably failing.
Change your error function to this, to debug what has happened:
'error': function(jqXHR, status, error) {
console.log(status);
console.log(error);
}
You might have an error in server-side code somewhere or you're calling the wrong PHP script perhaps?
I have read a few posts on fail parameters for a JQuery Ajax call, but none that have directly answered my question. If you want to read up on my jumping off point here, this post would be a good start:
jquery: is there a fail handler for $.post in Jquery?
My problem is that there are a handful of things that may cause my application to fail - if my script returns false the above method would work just fine for me (if I understand it correctly), but most of the time my script will fail by kicking out an exception that I handle using the Zend Framework. I would prefer to return the exception so that I can provide the user with a more detailed message. Is it possible to have my PHP script return a value while still letting the Ajax call know that it was a failure?
Sure you can. First of all you need to categorize you errors, for example:
Fatals
Exceptions
false / error status
I would advise you to take as a return value for correct and with no errors processing - 0. In all other case that would be an error.
Another useful advise would be to use JSON as a client-server conversation.
In PHP it would be:
function prepareJSONResponse($code, $message, array $extra = array())
{
return json_encode(array_merge(
$extra, array(
'code' => (int) $code,
'message' => $message)));
}
In this case you could pass error code and message, and additional params in $extra, for example, for this call:
prepareJSONResponse(1, 'Not enough data passed', array('debug' => true));
Response from server side would be:
{code:1,message:'Not enough data passed','debug': true}
For the client side you need a wrapper function for $.ajax:
// calback(result, error);
function call(url, params, callback)
{
if (typeof params == 'undefined') {
params = {};
}
$.ajax({
'type' : "POST",
'url' : url,
'async' : true,
'data' : params,
'complete' : function(xhr) {
if (xhr.status != 200) {
if (typeof callback == 'function') {
callback(xhr.responseText, true);
}
} else {
if (typeof callback == 'function') {
callback(xhr.responseText, false);
}
}
}
});
}
and function to validate JSON, in order if the corrupted format comes.
function toJSON(data){
try {
data = JSON.parse(data);
} catch (err) {
data = { 'code' : -999, 'message' : 'Error while processing response' };
}
if (typeof data.debug != 'undefined') {
console.log(data.debug);
}
return data;
}
Wrap in try-catch you code, and in catch statement do something like:
try {
...
} catch (Exception $e) {
exit(prepareJSONResponse(1, $e->getMessage(), array(
'debug' => 'There was an error while I were processing your request')));
}
The result would be that you receive debug information in browser console, and could handle errors / exception (prepareJSONResponse()) and fatals (by reading HTTP-status header, if it's not 200, then there was error).
Hope thats what you asked about.