来自MySQL结果的JSON对象[关闭]

From my understanding, when making Ajax call to server side (PHP), it can be specified what type data the call will allow. I often see this being a JSON object.

Is there any other object type that this Ajax call can specify?

It makes sense since JSON is for Javascript so it would want its data in corresponding format but why not just use fetched query result object directly like how it's done on PHP view?

JavaScript and PHP communicate with each other over HTTP with raw data (you can think of them as plain text strings). Any data conversion is handled by the language interpreter when it receives this raw data. Thus even though it seems like you're passing JavaScript objects back and forth, you're really just passing JSON-formatted strings and your JavaScript library is probably parsing them automatically.

PHP can only return data from an HTTP request by emission of this raw data which usually results in string conversion. So say you had a query result object -- you would do:

echo $result_object

This could be anything; maybe it would echo out "Object (#1Result)" -- or whatever the toString method of the object is. This is not very useful on the JavaScript side.

I'm not sure what you mean when you say PHP view, but I imagine that you have some sort of template engine. This is written in PHP and can do operations on the php objects itself to get the data it needs to emit in some format (probably HTML).

If your question is why is it better to use JSON as opposed to HTML -- well there are a lot of pros and cons regardless of your choice. JSON is generally a good choice because of its compact size and simplicity. If you wanted to actually display raw HTML it could be easier to just send the HTML instead of recreating it from JSON on the client.

Since the AJAX call is being fired by a javascript function (isn't is?) and the result is being returned to a javascript callback function you are working with javascript (at client side).

You can return JSON, plain text, XML... but then you have to care what format are you working with and at this point JSON is the best option IMHO.