0

I have the followin PHP file (a function of it):

public function get_hotels(){
   $hoteles = new HotelModel();
   $query = "SELECT * FROM hotel";
   $hoteles = $hoteles->execute_query($query);
   echo json_encode($hoteles);
}

And this is my jQuery script:

$.ajax({
   type: "POST",
   url: "index.php?controller=ExcursionTypes&action=get_hotels",
   dataType:"json",
   success: function(response){
    alert(typeof (response[0].hotel_name));
    //$("#pickups_fields").html(response[0].hotel_name);
   },
   error:function(response){
    alert("ERROR");
   }
});

Firebug throws me this JSON:

[{"id_hotel":"1","hotel_name":"Apt.Playa del Ingles", "hotel_phone":"928762629",
"hotel_corporation_id":"1","hotel_state_id":"1"},
{"id_hotel":"2","hotel_name":"LZ",
"hotel_phone":"928762629","hotel_corporation_id":"1", 
"hotel_state_id":"2"}]

I want to read both hotel_name fields and I can't.

I'm sure you're giving me the solution or a link to solve it.

Although I'm looking for it too.

3 Answers 3

1

Javascript is case sensitive, so you should write dataType, not datatype.

JSON that you get as a response is correct and response[0].hotel_name would work, but because you mistyped dataType, the response is not parsed as a JSON and therefore you can't access it the way you did.

Sign up to request clarification or add additional context in comments.

1 Comment

I've change the datatype into dataType and it's still throwing me the string "ERROR" in the "error:" property
0

Try iterating over the objects in response, like this:

$.ajax({
    type: "POST",
    url: "index.php?controller=ExcursionTypes&action=get_hotels",
    dataType:"json",
    success: function(response){
        $.each(response, function(i, hotel) {
            alert(typeof (hotel.hotel_name));
            //$("#pickups_fields").html(hotel.hotel_name);
        });
    },
    error:function(response){
        alert("ERROR");
    }
});

8 Comments

Didn't notice the dataType issue, so amended that. I still think this is the approach OP should take though.
I've change the datatype into dataType and it's still throwing me the string "ERROR" in the "error:" property
Try using $.getJSON() instead and see if that works.
it seems that the dataType:"json", line contains an error because when I comment it it shows me what I code in the success statement
that would mean that the response couldn't be parsed as JSON, which is strange, because the JSON you've posted is correct.
|
0
$.ajax({
    type: "POST",
    url: "index.php?controller=ExcursionTypes&action=get_hotels",
    dataType:"json",
    success: function(response){
        for(key in response) {
            alert(typeof (response.hotel_name[key]));
        });
    },
    error:function(response){
        alert("ERROR");
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.