0

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:

  1. How does $.ajax(), in simple terms, know which result to take, if at all? What if I had two items that are of the same dataType in the same PHP file? An error occurred; I initially thought it would just take the first item and ignore the second json_encodeed result. Then I tried echo-ing two items w/ different dataTypes. Again, I assumed that it would simply take the one that suits the dataType I specified and ignore the other. However, an error occurred too.
  2. 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?
Richard
  • 7,037
  • 2
  • 23
  • 76
  • 2
    You are getting an error because you are declaring your output to be JSON in the header but not returning valid JSON. See the possible duplicate for a possible solution. – Nick Feb 18 '19 at 02:24
  • @Nick I know, but I did do: `echo 'a'; echo json_encode(array('a'=>'b'));`... The second one is a JSON, right? I do know that I can just pass all the results I want in an array first before `echo json_encode`, but my example above did contain a `JSON` object and a string `'a'`. So I was expecting that it would simply ignore the string part and take the JSON. – Richard Feb 18 '19 at 02:29
  • Note, `"\"a\""` is valid `JSON` `console.log(JSON.parse("\"a\""))` – guest271314 Feb 18 '19 at 02:33
  • The problem is that your output as a **whole** must be valid JSON, and if you echo an `a` in front of it, it isn't. If you just `echo json_encode(array('a'=>'b'));` it will be valid JSON and your success routine will run – Nick Feb 18 '19 at 02:34
  • You can only return one JSON object `{....}` not `a{...}{...}` Therefore the only output you can have is a single `echo json_encode(...);` call. – ArtisticPhoenix Feb 18 '19 at 02:34
  • @WealthyPlayer The thing is that as you said your response will contains a string 'a' and a correct formatted JSON BUT jQuery will evaluate : 'a' + your JSON. So, it is a malformatted JSON and then throw an error as stated in the documentation. ```The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.``` – Geoffrey Migliacci Feb 18 '19 at 02:34
  • 1
    @guest271314 yes `"a"` is on it's own, but `"a" { "a" : "b" }` is not – Nick Feb 18 '19 at 02:35
  • @Nick Yes, the comment is just a note that a properly escaped string, without `{}` or `[]` included in the string is valid `JSON`. An "ordinary" `HTTP` request does not expect more than one response. `HTTP` is not a streaming protocol, either from client to server or server to client, though `EventSource` and `WebSocket` can be used to stream data to the client, `HTTP/2` is perhaps more suited to "streaming" data, depending on the application. – guest271314 Feb 18 '19 at 02:37
  • @Myerffoeg I see! That makes sense. Could you put that as an answer, please? – Richard Feb 18 '19 at 02:43
  • @Nick I see, thank you! – Richard Feb 18 '19 at 02:46

0 Answers0