I've read this and I've gotten insight on what textstatus of success: function(textstatus) gives. Apparently, there's a set amount of response that is generated automatically based on what happened when I called $.ajax(), with one being success, right?
However, I've still got questions on the success: function(data) part. I specified dataType: 'json' in my $.ajax() and as expected, I had to echo json_encode in my PHP file to get the results I wanted. However, I was curious so I tried to do the following:
<?php
    header('Content-Type: application/json');
    echo ('a');
    echo json_encode(array('a' => 'b'));
    echo json_encode(array('c' => 'd'));
?>
Then, I tried alerting inside my success function. It didn't work, so I guessed that an error occurred. Then, I tried placing an error: function() { alert('hi'); } and it did alert 'hi', indicating that it indeed detected an error. Then, I also tried to echo('a') with echo json_encode(array('a' => 'b'));. I expected it to return the JSON-encoded result as I did specify that I was expecting dataType: 'json', but oddly, it returned an error.
My questions and assumptions are:
- How does $.ajax(), in simple terms, know which result to take, if at all? What if I had two items that are of the samedataTypein the same PHP file? An error occurred; I initially thought it would just take the first item and ignore the secondjson_encodeed result. Then I triedecho-ing two items w/ differentdataTypes. Again, I assumed that it would simply take the one that suits thedataTypeI specified and ignore the other. However, an error occurred too.
- In relation to the first question, although probably unnecessary as I can simply pass all the results I want first in an array, how do I pass two different (say two arrays) results from one single $.ajax()call? Or is that plain impossible or terribly inelegant?
