Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Question: How can I response a JSON with render='json',and set the header
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Zane  
View profile  
 More options Feb 20 2012, 9:55 am
From: Zane <yaoaz...@gmail.com>
Date: Mon, 20 Feb 2012 06:55:26 -0800 (PST)
Local: Mon, Feb 20 2012 9:55 am
Subject: Question: How can I response a JSON with render='json',and set the header

Sample:
@view_config(permission = 'view',route_name='addUser', renderer='json',
custom_predicates=(allowed_methods('POST'),))
def add(request):
     post_data = request.json_body
     email = post_data['email']
     headers = remember(request, email, max_age='86400')

     /* How to response this header together*/
     return {'succeed':True}

Thanks
Zane


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris McDonough  
View profile  
 More options Feb 20 2012, 10:07 am
From: Chris McDonough <chr...@plope.com>
Date: Mon, 20 Feb 2012 10:07:14 -0500
Local: Mon, Feb 20 2012 10:07 am
Subject: Re: Question: How can I response a JSON with render='json',and set the header

On Mon, 2012-02-20 at 06:55 -0800, Zane wrote:
> Sample:
> @view_config(permission = 'view',route_name='addUser',
> renderer='json', custom_predicates=(allowed_methods('POST'),))
> def add(request):
>      post_data = request.json_body
>      email = post_data['email']
>      headers = remember(request, email, max_age='86400')

>      /* How to response this header together*/
>      return {'succeed':True}

I think this is what you're asking:

request.response.headers['Foo'] = 'bar'

- C


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zane  
View profile  
 More options Feb 20 2012, 10:34 am
From: Zane <yaoaz...@gmail.com>
Date: Mon, 20 Feb 2012 07:34:11 -0800 (PST)
Local: Mon, Feb 20 2012 10:34 am
Subject: Re: Question: How can I response a JSON with render='json',and set the header

Thanks @Chris

Yes.This is what I want, but now I receive a string "{'succeed':True}", and
before I add header in response is a object  {'succeed':True} ( I using
Ajax)
I guess this cause the view_config(renderer='json')

But How can I add header and return a Obj not a string.

Sorry~ I'm a newer.And Thank you.
Zane


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris McDonough  
View profile  
 More options Feb 20 2012, 10:46 am
From: Chris McDonough <chr...@plope.com>
Date: Mon, 20 Feb 2012 10:46:35 -0500
Local: Mon, Feb 20 2012 10:46 am
Subject: Re: Question: How can I response a JSON with render='json',and set the header

On Mon, 2012-02-20 at 07:34 -0800, Zane wrote:
> Thanks @Chris

> Yes.This is what I want, but now I receive a string
> "{'succeed':True}", and before I add header in response is a object
> {'succeed':True} ( I using Ajax)
> I guess this cause the view_config(renderer='json')

> But How can I add header and return a Obj not a string.

> Sorry~ I'm a newer.And Thank you.
> Zane

I'm afraid I don't understand, sorry.

- C


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zane  
View profile  
 More options Feb 20 2012, 11:05 am
From: Zane <yaoaz...@gmail.com>
Date: Mon, 20 Feb 2012 08:05:03 -0800 (PST)
Local: Mon, Feb 20 2012 11:05 am
Subject: Re: Question: How can I response a JSON with render='json',and set the header

Oh~ Excuse my English~

May be I know the reason of my fault,
I am using  pyramid.security

from pyramid.security import authenticated_userid, remember, forget

@view_config(route_name='user', renderer='json')
     ..............
     headers = remember(request, email, max_age='86400')

     request.response.headers = headers
     # It would lost the Content-Type:application/json here
     # so when I add header like this way, it will return a string.
    return {'succeed':True}

And before I replace the header, the render='json' will help me to add
the Content-Type.

Thanks
Zane


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
cheeleong  
View profile  
 More options Feb 20 2012, 11:08 am
From: cheeleong <klrkdek...@gmail.com>
Date: Tue, 21 Feb 2012 00:08:58 +0800
Local: Mon, Feb 20 2012 11:08 am
Subject: Re: Question: How can I response a JSON with render='json',and set the header

Try this

@view_config(route_name='user', renderer='json')
def user(request):
  headers = remember(request, email)
  request._response_headerlist_set(headers)
  return {'foo':'bar'}

Good luck.

Regards,
CL Chow

Using Gmail? Please read this important notice:
http://www.fsf.org/campaigns/jstrap/gmail?8483


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zane  
View profile  
 More options Feb 20 2012, 11:16 am
From: Zane <yaoaz...@gmail.com>
Date: Mon, 20 Feb 2012 08:16:36 -0800 (PST)
Local: Mon, Feb 20 2012 11:16 am
Subject: Re: Question: How can I response a JSON with render='json',and set the header

It's work  *:*)

Thank you @ yasaikun and  Chris McDonough


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
cheeleong  
View profile  
 More options Feb 20 2012, 11:25 am
From: cheeleong <klrkdek...@gmail.com>
Date: Tue, 21 Feb 2012 00:25:36 +0800
Local: Mon, Feb 20 2012 11:25 am
Subject: Re: Question: How can I response a JSON with render='json',and set the header

No problem :)

Regards,
CL Chow

Using Gmail? Please read this important notice:
http://www.fsf.org/campaigns/jstrap/gmail?8483


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »