-1

I have this code:

$.ajax({
                                url: '{{ route('frontend.validation') }}?sex=' + $(".sex").val() + '&date=' + $(".date").val() + '&hour=' + $(".hour").val()+ '&track=' + $(".track").val(),
                                type: 'get',
                                dataType: 'json',
                                headers: {
                                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                                },
                                cache: false,
                                success: function (response) {
                                    $.each(response, function (key, value) {

                                        console.log(response);
                                    })
                                }
                            });

In result I have:

{"status":"ok","message":"Twoja rezerwacja zosta\u0142a zrealizowana"}

in console I have:

[Log] {status: "ok", message: "Twoja rezerwacja została zrealizowana"} (projekt1.test, line 284) [Log] {status: "ok", message: "Twoja rezerwacja została zrealizowana"} (projekt1.test, line 284)

I need check my status. If it's "ok" - then message I want show in alert box.

When status = "error" them I want show alert with "Sorry, we have error with yours reservation".

How can I make it?

Sammitch
  • 30,782
  • 7
  • 50
  • 77
trelloka
  • 17
  • 5
  • The ajax request has the option of `dataType: 'json'` on it, so the response is already going to be parsed. – Taplar Jun 09 '20 at 22:00

4 Answers4

2

u have already json object so you don't need to parse. Remove each method ,it uses keys count so you get alert two times .

success: function (response) {
   if(response.status==="ok"){
      alert("ok")   
    }
    else if(response.status==="error"){
      alert("error")
    }
 }
    
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
  • 1
    because you did put in each method. and your response have two keys. remove each method just take inside them. I edited in answer success method check it – mr. pc_coder Jun 09 '20 at 21:55
-1

try this

$.ajax({
        url: '{{ route('frontend.validation') }}?sex=' + $(".sex").val() + '&date=' + $(".date").val() + '&hour=' + $(".hour").val()+ '&track=' + $(".track").val(),
        type: 'get',
        dataType: 'json',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
        },
        cache: false,
        success: function (response) {
            $.each(response, function (key, value) {

                
                var obj = JSON.parse( response );

                console.log(obj['status'])

            })
        }
    });
ikenas
  • 411
  • 3
  • 11
-1

From Mozilla documentation

The JSON.parse() takes the string text input and returns the object corresponding to the given JSON text.

Use JSON.parse(response) and then read the index "status" of the object.

Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
-1

The question is already answered HERE LINK

$.ajax({
    success: function(response){  
        $.each(response, function (key, value) {
            if(response.status==="ok"){
                alert("Twoja rezerwacja została zrealizowana");   
            }
            else if(response.status==="error"){
                alert("Sorry, we have error with yours reservation");
            }
        })
    },
    error: function(response) { 
        alert("Sorry, we have error with yours reservation"); 
    }
});
return False;
....
Shubham K.
  • 67
  • 1
  • 9