Ok so this is my js on the page:

$('#test').live('click', test);

        
        function test() {

        var data = $('#add_award').serialize();

        $.ajax({
        type: "post",
        url: "/manage/awards/",
        data: data,
        dataType: 'json',
        success: function(response){
                   alert(response.data.id);


        }
        });

    }

No alert happens.

The response shows up as:
{"data":{"title":"","year_rec":{"year":"2010"},"description":"","id":"4cec63
725d2","profile_id":"4b40eea7-2608-4a3b-9c24-7cb04adcd75b"},"status":true,"h
tml":"<p>this is a good test<\/p>\r\n"}<p>this is a good test</p>

Not sure why the html code gets repeated after the json part. I am using
cake and so far nobody has been able to assit me so im going on trial and
error.

So to test out the alert response I removed the html part leaving only data
and success  so I get a Firebug JSON tab in the response:

{"data":{"title":"","year_rec":{"year":"2010"},"description":"","id":"4ff9c8
f01b8","profile_id":"4b40eea7-2608-4a3b-9c24-7cb04adcd75b"},"status":true}

And alert(response.data.id); => 4ff9c8f01b8

So its just the darn HTML which is killing me here. Cant figure out how to
get just the HTML to apear in the JSON and not display after the code....

Dave

 

-----Original Message-----
From: Michael Geary [mailto:m...@mg.to] 
Sent: January-11-10 4:36 PM
To: jquery-en@googlegroups.com
Subject: Re: [jQuery] Re: Ajax forms help

The JSON part of your server response looks fine if you take out the two
line breaks - I assume those are just an artifact of posting with Outlook,
and your actual JSON response is all on one line.

But why does it have the HTML content repeated after the last } that closes
the JSON data? You don't want that.

Also:

> Is response.html saying to display it as html?

No! response.html says "take the object named 'response' and give me its
property named 'html'." It has nothing to do with displaying anything or
specifying its format.

Here, let's take your JSON output and paste it into www.jsonlint.com so it's
easier to read:

{
    "data": {
        "title": "",
        "year_rec": {
            "year": "2010"
        },
        "description": "",
        "id": "0936d6115e4",
        "profile_id": "4b40eea7-2608-4a3b-9c24-7cb04adcd75b"
    },
    "status": true,
    "html": "<p>this is a good test<\/p>\r\n"
}

You see, it's an object with three properties, named 'data', 'status', and
'html'. The 'data' property is itself an object with properties of its own.

So if you have a variable named 'response' (as in Ibatex's example), then
you can get to these various properties like so:

response.html (the HTML code)
response.data.id (the id)
response.data.year_rec.year (the year)

What you do with those is then up to you, as in Ibatex's earlier example.

BTW do you have the Fiddler debugging proxy server and its JSON plugin (and
standalone JSON viewer)? For the work you're doing now, you *need* these.

http://www.fiddler2.com/
http://www.fiddler2.com/Fiddler2/extensions.asp

-Mike


On Mon, Jan 11, 2010 at 11:41 AM, Dave Maharaj :: WidePixels.com
<d...@widepixels.com> wrote:


        My response comes back from the server looking like this: (don't
think its
        right to begin with)
        
        
{"data":{"title":"","year_rec":{"year":"2010"},"description":"","id":"0936d6
        
115e4","profile_id":"4b40eea7-2608-4a3b-9c24-7cb04adcd75b"},"status":true,"h
        tml":"<p>this is a good test<\/p>\r\n"}<p>this is a good test</p>
        
        But how would I display only the html section and clean it so its
not all
        slashed and escaped?
        
        Thanks
        
        Dave
        
        -----Original Message-----
        From: MorningZ [mailto:morni...@gmail.com]
        Sent: January-11-10 11:44 AM
        To: jQuery (English)
        Subject: [jQuery] Re: Ajax forms help
        
        "But if I am returning json I cant return my normal html"
        
        I don't understand why...
        
        I pretty much exclusively use JSON back and forth in my jQuery AJAX
calls
        and have a standard JSON object i return:
        
        {
          HasError: <boolean>,
          Message: <string>.
          Data: <object>,
          Count: <integer>
        }
        
        Many times i'll return HTML on the data property there....  i think
the
        problem you are having is how you are creating the JSON return
string, maybe
        that's not the right way to do it in PHP ??  (to note, i'm a .NET
guy, i
        don't know what the PHP way is)
        
        On Jan 11, 10:06 am, "Dave Maharaj :: WidePixels.com"
        <d...@widepixels.com> wrote:
        > Right on...looks easy enough.
        >
        > Is response.html saying to display it as html? I tried making an
array
        > to pass as json from php then json_encode it so my arrr was
$response
        > = array('status' => true , 'view' => 'all my html code went here')
        >
        > But when I returned my view data from the array it was all slahsed
<//
        > "// /" / / like that.
        >
        > -----Original Message-----
        > From: Ibatex [mailto:mjgris...@gmail.com]
        > Sent: January-11-10 4:35 AM
        > To: jQuery (English)
        > Subject: [jQuery] Re: Ajax forms help
        >
        > The beauty of json is that you can transfer alot of different data
in
        > an organized way. You can very easily send back success/failure
status
        > along with the html.
        >
        > success: function(response) {
        >                         // Response was a success
        >                         if (response.status) {
        >                                 //update my target div
        >
$('#target_div').html(response.html);
        >                         // Response contains errors
        >                         } else {
        >                                 // return the form with the errors
        >
        >                         }
        >
        > On Jan 10, 5:00 pm, "Dave Maharaj :: WidePixels.com"
        > <d...@widepixels.com> wrote:
        > > I need some help with a form.
        >
        > > I submit the form fine, my problem is depending on the success
or
        > > failure of the form being saved. I will try to explain as simple
as
        > > possible
        >
        > > form is in its own div <form here ..... >
        >
        > > <div target> saved data will appear here</div>
        >
        > > So what I need is i guess json success true or false response
from
        > > the form being saved or not then in my success
        >
        > > success: function(response) {
        > >                         // Response was a success
        > >                         if (response) {
        > >                                 //update my target div
        > >                         // Response contains errors
        > >                         } else {
        > >                                 // return the form with the
errors
        >
        > >                         }
        >
        > > But if I am returning json I cant return my normal html after
the
        > > successful save, and if I return my response as html there is no
way
        > > to tell the js what to do.
        > > The form or the target is html code
        >
        > > How can I tell what the response was (true or false) and return
the
        > > appropriate html code for the appropriate form or target?
        >
        > > Thanks
        >
        > > Dave
        > No virus found in this incoming message.
        > Checked by AVG -www.avg.com
        > Version: 9.0.725 / Virus Database: 270.14.130/2607 - Release Date:
        > 01/10/10 16:05:00
        No virus found in this incoming message.
        Checked by AVG - www.avg.com
        
        Version: 9.0.725 / Virus Database: 270.14.130/2607 - Release Date:
01/11/10
        04:05:00
        
        


No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.725 / Virus Database: 270.14.130/2607 - Release Date: 01/11/10
04:05:00



Reply via email to