I'm not positive how jQuery handles mismatched response expectations. Testing it for yourself would be the best way to go, if you're worried about it. But your best case is to specify your expected return, if only to keep a record somewhere of what you should be getting back.
On Feb 3, 12:06 pm, Professor B <[email protected]> wrote: > @Eric I think I get it. > > So I assume -- again, using this example -- if your server returned > json content AND the appropriate header, you were expecting json and > you did NOT pass 'json' as the 4th param to $.post(), then data.name > etc would be undefined. Right? > > And conversely, if your server returned JSON but failed to indicate > the content-type as json but you DID tell $.post() it's JSON, > then .... bla bla bla it would work. Right? I should test it and see > for myself, I know. > > @maggus.staab Thanks for the tip, I will have a look. > > On Feb 3, 11:52 am, Eric Garside <[email protected]> wrote: > > > Basically, the 4th parameter tells jQuery what to do with the response > > data. > > > JSON indicates you're expecting a JSON string as the post response. > > 'xml' indicates you want the return text parsed as XML (true AJAX). > > 'html' means it's coming in as, I think a jquery object. And text is > > just text. Those are the basic ones I've seen used most of the time. > > > On Feb 3, 11:15 am, "maggus.staab" <[email protected]> wrote: > > > > at api.jquery.com there is a well documented version.. > > > > @api doc devel: > > > would be nice if we can link to a specific api page... > > > > On 3 Feb., 16:55, Professor B <[email protected]> wrote: > > > > > I have stared at the docs for a while and still don't understand the > > > > purpose of the option 4th parameter > > > > in the $.post() method. The wording and examples > > > > athttp://docs.jquery.com/Ajax/jQuery.postseemodd. E.g: > > > > > [quote] > > > > Gets the test.php page contents which has been returned in json format > > > > (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>) > > > > > $.post("test.php", { func: "getNameAndTime" }, > > > > function(data){ > > > > alert(data.name); // John > > > > console.log(data.time); // 2pm > > > > }, "json"); > > > > [/quote] > > > > > We submit a POST request to test.php with parameter func = > > > > 'getNameAndTime' -- presumably for test.php's benefit so it can call > > > > that function, right? I think that's a little obscure in this example. > > > > > Then we have an anonymous callback function that fires upon completion > > > > of a successful xhr request, its input parameter being 'data' which is > > > > the response body of the xhr object, right? > > > > > In this instance, 'data' is an object -- a hash, an associative array, > > > > what have you. So if that's what it is, then it is what it is, so to > > > > speak. Is it not? I would expect that internally, JQuery would detect > > > > the content-type header indicating JSON and eval the xhr response body > > > > automatically. The server side would (and should) be responsible for > > > > sending the proper content-type. > > > > > Am I to understand that in this example test.php might as well send > > > > plain old text/html, and the type hint "json" instructs JQuery to eval > > > > it?

