Change header when returning a JsonModel

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Change header when returning a JsonModel

Oscar Fanelli
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar

Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Stefano Torresi
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Oscar Fanelli
Thanks Stefano,
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -1000);

    return new JsonModel($result);

but it doesn’t work.

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:

Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.

Cheers.

Stefano Torresi


2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar



Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Sascha-Oliver Prolic
Hi Oscar,

you should attach to:

SendResponseEvent::EVENT_SEND_RESPONSE with any priority higher than -1000, that should do the trick.


Sascha-Oliver Prolic


2014-04-05 19:54 GMT+02:00 Oscar Fanelli <[hidden email]>:
>
> Thanks Stefano,
> I tried with:
>     $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
>         $response = $this->getResponse();
>         $headers = $response->getHeaders();
>         $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
>         $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
>         $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
>         $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
>         $response->setHeaders($headers);
>     }, -1000);

>
>     return new JsonModel($result);
>
> but it doesn’t work.
>
> Oscar
>
> ---
>
> Oscar Fanelli
> Gamempire.it - Creatore e Direttore esecutivo
> Tom's Hardware Italia - Sviluppatore
>
> Email: [hidden email]
> Email secondaria: [hidden email]
> Skype: gamempire
> Tel: 3388696167
>
> Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:
>
> Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
> The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.
>
> Cheers.
>
> Stefano Torresi
>
>
> 2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
>>
>> Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534
>>
>> Thanks a lot,
>> Oscar
>>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Stefano Torresi
In reply to this post by Oscar Fanelli
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Sascha-Oliver Prolic
In reply to this post by Sascha-Oliver Prolic
By the way, can you please create a ticket on github for this? I am pretty sure that is must also work when you do it directly in your controller and return a json model. If not, I think you probably found a bug!

Sascha-Oliver Prolic


2014-04-05 20:55 GMT+02:00 Sascha-Oliver Prolic <[hidden email]>:
Hi Oscar,

you should attach to:

SendResponseEvent::EVENT_SEND_RESPONSE with any priority higher than -1000, that should do the trick.


Sascha-Oliver Prolic


2014-04-05 19:54 GMT+02:00 Oscar Fanelli <[hidden email]>:

>
> Thanks Stefano,
> I tried with:
>     $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
>         $response = $this->getResponse();
>         $headers = $response->getHeaders();
>         $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
>         $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
>         $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
>         $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
>         $response->setHeaders($headers);
>     }, -1000);

>
>     return new JsonModel($result);
>
> but it doesn’t work.
>
> Oscar
>
> ---
>
> Oscar Fanelli
> Gamempire.it - Creatore e Direttore esecutivo
> Tom's Hardware Italia - Sviluppatore
>
> Email: [hidden email]
> Email secondaria: [hidden email]
> Skype: gamempire
> Tel: 3388696167
>
> Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:
>
> Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
> The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.
>
> Cheers.
>
> Stefano Torresi
>
>
> 2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
>>
>> Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534
>>
>> Thanks a lot,
>> Oscar
>>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Sascha-Oliver Prolic
In reply to this post by Stefano Torresi
Yeah right, if you use an older version of the skeleton app, it might not work appriopritly, as there are some changes in the index.php file.

Sascha-Oliver Prolic


2014-04-05 20:58 GMT+02:00 Stefano Torresi <[hidden email]>:

Hmm that's odd. I tried that very same code on a clean skeleton and it worked...

Il 05/apr/2014 19:54 "Oscar Fanelli" <[hidden email]> ha scritto:

Thanks Stefano,
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -1000);

    return new JsonModel($result);

but it doesn’t work.

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:

Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.

Cheers.

Stefano Torresi


2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar




Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Oscar Fanelli
Uh! I’m going to see the new skeleton, maybe I have an old one. I think I can see this one, true? https://github.com/zendframework/ZendSkeletonApplication

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 05/apr/2014, alle ore 21:00, Sascha-Oliver Prolic <[hidden email]> ha scritto:

Yeah right, if you use an older version of the skeleton app, it might not work appriopritly, as there are some changes in the index.php file.

Sascha-Oliver Prolic


2014-04-05 20:58 GMT+02:00 Stefano Torresi <[hidden email]>:

Hmm that's odd. I tried that very same code on a clean skeleton and it worked...

Il 05/apr/2014 19:54 "Oscar Fanelli" <[hidden email]> ha scritto:

Thanks Stefano,
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -1000);

    return new JsonModel($result);

but it doesn’t work.

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:

Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.

Cheers.

Stefano Torresi


2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar





Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Stefano Torresi
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Oscar Fanelli
@Stefano 2.3.x (3d4c5a2)
@Sascha the index.php is the same of the skeleton
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\ResponseSender\SendResponseEvent::EVENT_SEND_RESPONSE, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -999);

but the same it’s not working.
However, I have opened a ticket: https://github.com/zendframework/zf2/issues/6087

Oscar

Il giorno 05/apr/2014, alle ore 21:10, Stefano Torresi <[hidden email]> ha scritto:

I'd rather check the framework version, I don't think the skeleton itself has something to do with this...

Il 05/apr/2014 21:05 "Oscar Fanelli" <[hidden email]> ha scritto:
Uh! I’m going to see the new skeleton, maybe I have an old one. I think I can see this one, true? https://github.com/zendframework/ZendSkeletonApplication

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 05/apr/2014, alle ore 21:00, Sascha-Oliver Prolic <[hidden email]> ha scritto:

Yeah right, if you use an older version of the skeleton app, it might not work appriopritly, as there are some changes in the index.php file.

Sascha-Oliver Prolic


2014-04-05 20:58 GMT+02:00 Stefano Torresi <[hidden email]>:

Hmm that's odd. I tried that very same code on a clean skeleton and it worked...

Il 05/apr/2014 19:54 "Oscar Fanelli" <[hidden email]> ha scritto:

Thanks Stefano,
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -1000);

    return new JsonModel($result);

but it doesn’t work.

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:

Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.

Cheers.

Stefano Torresi


2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar






Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Oscar Fanelli
Whoaaaaa I found the answer. That code was not executed because it doesn't recognize this line:

if ($this->getRequest()->isXmlHttpRequest()) {

very strange but... ok!
Maybe ZF2 doesn’t recognize it as an isXmlHttpRequest if it comes from a different subdomain…

Oscar

Il giorno 05/apr/2014, alle ore 21:23, Oscar Fanelli <[hidden email]> ha scritto:

@Stefano 2.3.x (3d4c5a2)
@Sascha the index.php is the same of the skeleton
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\ResponseSender\SendResponseEvent::EVENT_SEND_RESPONSE, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -999);

but the same it’s not working.
However, I have opened a ticket: https://github.com/zendframework/zf2/issues/6087

Oscar

Il giorno 05/apr/2014, alle ore 21:10, Stefano Torresi <[hidden email]> ha scritto:

I'd rather check the framework version, I don't think the skeleton itself has something to do with this...

Il 05/apr/2014 21:05 "Oscar Fanelli" <[hidden email]> ha scritto:
Uh! I’m going to see the new skeleton, maybe I have an old one. I think I can see this one, true? https://github.com/zendframework/ZendSkeletonApplication

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 05/apr/2014, alle ore 21:00, Sascha-Oliver Prolic <[hidden email]> ha scritto:

Yeah right, if you use an older version of the skeleton app, it might not work appriopritly, as there are some changes in the index.php file.

Sascha-Oliver Prolic


2014-04-05 20:58 GMT+02:00 Stefano Torresi <[hidden email]>:

Hmm that's odd. I tried that very same code on a clean skeleton and it worked...

Il 05/apr/2014 19:54 "Oscar Fanelli" <[hidden email]> ha scritto:

Thanks Stefano,
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -1000);

    return new JsonModel($result);

but it doesn’t work.

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:

Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.

Cheers.

Stefano Torresi


2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar







Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Antoine Hedgecock
There is a module for that …..


On 05 Apr 2014, at 21:29, Oscar Fanelli <[hidden email]> wrote:

Whoaaaaa I found the answer. That code was not executed because it doesn't recognize this line:

if ($this->getRequest()->isXmlHttpRequest()) {

very strange but... ok!
Maybe ZF2 doesn’t recognize it as an isXmlHttpRequest if it comes from a different subdomain…

Oscar

Il giorno 05/apr/2014, alle ore 21:23, Oscar Fanelli <[hidden email]> ha scritto:

@Stefano 2.3.x (3d4c5a2)
@Sascha the index.php is the same of the skeleton
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\ResponseSender\SendResponseEvent::EVENT_SEND_RESPONSE, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -999);

but the same it’s not working.
However, I have opened a ticket: https://github.com/zendframework/zf2/issues/6087

Oscar

Il giorno 05/apr/2014, alle ore 21:10, Stefano Torresi <[hidden email]> ha scritto:

I'd rather check the framework version, I don't think the skeleton itself has something to do with this...

Il 05/apr/2014 21:05 "Oscar Fanelli" <[hidden email]> ha scritto:
Uh! I’m going to see the new skeleton, maybe I have an old one. I think I can see this one, true? https://github.com/zendframework/ZendSkeletonApplication

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 05/apr/2014, alle ore 21:00, Sascha-Oliver Prolic <[hidden email]> ha scritto:

Yeah right, if you use an older version of the skeleton app, it might not work appriopritly, as there are some changes in the index.php file.

Sascha-Oliver Prolic


2014-04-05 20:58 GMT+02:00 Stefano Torresi <[hidden email]>:

Hmm that's odd. I tried that very same code on a clean skeleton and it worked...

Il 05/apr/2014 19:54 "Oscar Fanelli" <[hidden email]> ha scritto:

Thanks Stefano,
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -1000);

    return new JsonModel($result);

but it doesn’t work.

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:

Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.

Cheers.

Stefano Torresi


2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar








Reply | Threaded
Open this post in threaded view
|

Re: Change header when returning a JsonModel

Oscar Fanelli
Nice, thanks, I will try it :)

Oscar

Il giorno 05/apr/2014, alle ore 21:45, Antoine Hedgecock <[hidden email]> ha scritto:

There is a module for that …..


On 05 Apr 2014, at 21:29, Oscar Fanelli <[hidden email]> wrote:

Whoaaaaa I found the answer. That code was not executed because it doesn't recognize this line:

if ($this->getRequest()->isXmlHttpRequest()) {

very strange but... ok!
Maybe ZF2 doesn’t recognize it as an isXmlHttpRequest if it comes from a different subdomain…

Oscar

Il giorno 05/apr/2014, alle ore 21:23, Oscar Fanelli <[hidden email]> ha scritto:

@Stefano 2.3.x (3d4c5a2)
@Sascha the index.php is the same of the skeleton
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\ResponseSender\SendResponseEvent::EVENT_SEND_RESPONSE, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -999);

but the same it’s not working.
However, I have opened a ticket: https://github.com/zendframework/zf2/issues/6087

Oscar

Il giorno 05/apr/2014, alle ore 21:10, Stefano Torresi <[hidden email]> ha scritto:

I'd rather check the framework version, I don't think the skeleton itself has something to do with this...

Il 05/apr/2014 21:05 "Oscar Fanelli" <[hidden email]> ha scritto:
Uh! I’m going to see the new skeleton, maybe I have an old one. I think I can see this one, true? https://github.com/zendframework/ZendSkeletonApplication

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 05/apr/2014, alle ore 21:00, Sascha-Oliver Prolic <[hidden email]> ha scritto:

Yeah right, if you use an older version of the skeleton app, it might not work appriopritly, as there are some changes in the index.php file.

Sascha-Oliver Prolic


2014-04-05 20:58 GMT+02:00 Stefano Torresi <[hidden email]>:

Hmm that's odd. I tried that very same code on a clean skeleton and it worked...

Il 05/apr/2014 19:54 "Oscar Fanelli" <[hidden email]> ha scritto:

Thanks Stefano,
I tried with:
    $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($this){
        $response = $this->getResponse();
        $headers = $response->getHeaders();
        $headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true');
        $headers->addHeaderLine('Access-Control-Allow-Origin', '*');
        $headers->addHeaderLine('Access-Control-Allow-Headers', 'Content-Type, *');
        $response->setHeaders($headers);
    }, -1000);

    return new JsonModel($result);

but it doesn’t work.

Oscar

---

Oscar Fanelli
Gamempire.it - Creatore e Direttore esecutivo
Tom's Hardware Italia - Sviluppatore

Email secondaria: [hidden email]
Skype: gamempire
Tel: 3388696167

Il giorno 03/apr/2014, alle ore 12:59, Stefano Torresi <[hidden email]> ha scritto:

Either attach to 'render' event with priority lower than -10000 or to 'finish' event with priority higher than -10000.
The latter is the priority at wich Zend\Mvc\SendResponseListener sends the response, so in your gist, where you're attaching to 'finish' at -100000, you're actually trying to change a response wich has already been sent.

Cheers.

Stefano Torresi


2014-04-03 9:48 GMT+02:00 Oscar Fanelli <[hidden email]>:
Hi guys, someone can help me with this problem? It’s a very huge problem for me and I will be very grateful if somebody can take a look: http://stackoverflow.com/questions/22767534/zf2-how-to-change-header-on-return-jsonmodel?noredirect=1#comment34786649_22767534

Thanks a lot,
Oscar