Skip to content
Advertisement

how to determine if the ajax response is an array of key value pair or a simple string response

I have a simple ajax function that sends data to the server and prints the response. the problem is some functions from PHP send the response as an array while some functions send the response as a simple string. For example, if the function was a success then the response is received as a key of response and a simple string if the response is a success while if any unauthorized error is found then an error key with possible error response is returned. I have different functions to perform according to the response type received. Till now I am able to check if a key exists in the response and this can determine whether the response has key-value pair. But this check fails when the response is a simple string instead of an array.

here is my PHP code in the server

public function destroy(Product $product) {
    $product->delete();
    return response()->json(<div class="alert alert-success">The data was deleted!</div>');
}

However, my middleware checks the request and sends a response as an array

public function handle(Request $request, Closure $next) {
    if (!auth()->user()->isAdmin()) {                    
         return response()->json(array('error'=> '<div class="alert alert-danger alert-dismissible fade show">You must be an admin to perform this action <button type="button" class="close" onclick="hide()">&times; </button></div>'),'response'=>'');
    }
    return $next($request);
}

Here is my jquery code for ajax

function disable(url,datatable,data,message="change the status",postmethod='POST') {
    $.confirm ({
        title: 'Confirmation please!',
        content: "This will "+ message+". Are you sure?",
        type: 'blue',
        buttons:{
            Yes: {
                btnClass: 'btn-blue',
                action: function() {
                $.ajax({
                    url:url,
                    method:postmethod,
                    data:data,
                    dataType:"JSON",
                    success:function(response) {
                        if ("error" in response && response.error!=''){
                            //works nice when the response is a key value pair but 
                            //show error if the response is a simple string
                            result(response.error);
                        } else if ("response" in response) {
                            result(response.response,datatable);
                        } else if ("success" in response) {
                            result(response.success,datatable);
                        } else {
                            result(response,datatable);
                        }
                    });
                }
            },
        }
    });
}

I get the following error when a simple string message is received instead of an object or key-value pair

Uncaught TypeError: Cannot use 'in' operator to search for 'error' in <div class="alert alert-success">Data deleted</div>

Advertisement

Answer

you can check for the type of data return by the ajax response by using the function typeof and perform necesssary action.

 success:function(response) {    
                 if(typeof response=="string")
                   {
                                        result(response);
                                        return
                     }
                      else if(typeof response=="object"){
                        if ("error" in response && response.error!=''){
                            your code to execute if the response is of type object
                        }
                   }
                    };
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement