Currently, I believe so. Prior to that was probably using XML (the
intent of AJAX's XMLHttpRequest), but it required a lot of work
needing to drill down to reading the content, and the response size of
XML was fairly large due to all the XML tags required. JSON doesn't
use such structural tags, so less overhead and the response size is
generally very small because of it (thus, your application get a
response quicker). JSON is already a Javascript object format so you
can very easily reference its content without needing to parse (much)
the data. Many dynamic AJAX applications already uses JSON as the
response data type. For example, Gmail.

On Mar 27, 4:19 pm, Link <ryanwjack...@gmail.com> wrote:
> Just out of curiosity, is this typically the best way?
>
> On Mar 27, 7:46 pm, James <james.gp....@gmail.com> wrote:
>
> > The best way is to use JSON as the dataType.
> > In PHP, all you need to use is the json_encode() function to convert a
> > PHP array to JSON. It's like an easy way to pass back an array full of
> > data back to the client that you can manipulate as regular Javascript
> > variables.
> > For example, your PHP code would look like:
>
> > $myArray = array(
> >      'firstName' => 'John',
> >      'lastName' => 'Doe',
> >      'error' => 0
> > );
> > echo json_encode($myArray);  // echo your response back
> > exit;
>
> > Then in your $.ajax success callback function:
> > function(data) {
> >      alert(data.firstName); // -> alerts 'John'
>
> > }
>
> > On Mar 27, 12:34 pm, Link <ryanwjack...@gmail.com> wrote:
>
> > > So I am working on an AJAX call to a PHP script which validates the
> > > data submitted in a form.  My question concerns the best way to get
> > > the data from PHP back to the jQuery script.  Should the dataType
> > > field be html or script?  I tried html and I know I could parse out
> > > what I need, but in the documentation it says script runs the code as
> > > a javascript script, but I'm not sure of the scope of this script.  I
> > > tried setting a variable name in the script and changing a global
> > > variable value, but I saw no change when I displayed the value with
> > > alert().  Am I missing something?
>
> > > Code below:
>
> > > $.ajax({
> > >                         type: "POST",
> > >                         url: "/register/index.php?action=validate",
> > >                         data: "email="+ email + "& pass1=" + pass1,
> > >                         dataType: "script",//"html",
> > >                         complete: function (XMLHttpRequest, textStatus) {
> > >                                 alert("complete");
> > >                         },
> > >                         success: function(del){
> > >                                 alert("success");
> > >                                 alert(del);
> > >                                 alert(email);
> > >                                 $("#email").addClass("error");
> > >                                 /*$('form#submit').hide();
> > >                                 $('div.success').fadeIn();*/
> > >                         },
> > >                         error: function (xhr, desc, exceptionobj) {
> > >                                 alert("error");
> > >                                 //alert(xhr.responseText);
> > >                                 alert(exceptionobj);
> > >                         }
> > >                 });
>
> > > I can make the PHP file return whatever I want.  What is the best in
> > > this situation?
>
> > > Thanks!
>
>

Reply via email to