2013-02-08 32 views
18

से केवल JSON को वापस कैसे करें I प्रोजेक्ट के लिए ज़ेंड फ्रेमवर्क 1.x का उपयोग कर रहा हूं। मैं कॉलर फ़ंक्शन के लिए केवल एक वेब सेवा रिटर्न बनाना चाहता हूं जो JSON स्ट्रिंग है। मैं Zend_Controller_Action का उपयोग करने की कोशिश की और उन तरीकों लागू:ज़ेन

1.

$this->getResponse() 
    ->setHeader('Content-type', 'text/plain') 
    ->setBody(json_encode($arrResult)); 

2.

$this->_helper->getHelper('contextSwitch') 
       ->addActionContext('nctpaymenthandler', 'json') 
       ->initContext(); 

3.

header('Content-type: application/json'); 

4.

$this->_response->setHeader('Content-type', 'application/json'); 

5.

echo Zend_Json::encode($arrResult); 
exit; 

6.

return json_encode($arrResult); 

7.

$this->view->_response = $arrResult; 

लेकिन जब मैं परिणाम प्राप्त करने के लिए प्रयोग किया जाता cURL, यह कुछ HTML टैग से घिरा हुआ JSON स्ट्रिंग के साथ लौट आए। फिर मैंने उपरोक्त विकल्पों के साथ Zend_Rest_Controller उपयोगकर्ता को करने का प्रयास किया। यह अभी भी सफलता नहीं मिली है।

पीएस .: ऊपर दिए गए अधिकांश तरीके स्टैक ओवरफ़्लो पर पूछे गए प्रश्न से हैं।

उत्तर

32

मुझे इस तरह से पसंद है!

//encode your data into JSON and send the response 
$this->_helper->json($myArrayofData); 
//nothing else will get executed after the line above 
+3

मैंने कुछ समय के लिए इस विधि का उपयोग किया है। मैं सभी अतिरिक्त कोड की आवश्यकता को समझ नहीं पा रहा हूं। जहां तक ​​मैं कह सकता हूं सहायक सहायक विधि आपके लिए सब कुछ संभालती है। – David

+0

इस कोड को कहां रखा जाए? नियंत्रक के कार्य समारोह में? –

+0

@ हरिसमहमूद आपके नियंत्रक की कार्रवाई इसे सही जगह है क्योंकि यह अनुरोध को संभालने और आउटपुट तैयार करने की अपनी भूमिका है। – Tim

7

मानक कोड टेम्पलेट के साथ सामग्री को लपेटने के लिए आपके कोड को लेआउट को अक्षम करने की आवश्यकता होगी। लेकिन एक बहुत आसान दृष्टिकोण सिर्फ होगा:

$this->getHelper('json')->sendJson($arrResult); 

JSON सहायक JSON के रूप में अपने चर सांकेतिक शब्दों में बदलना होगा, उचित हेडर सेट और आप के लिए लेआउट और दृश्य स्क्रिप्ट अक्षम।

9

आपको लेआउट को अक्षम करने और प्रतिपादन देखने की आवश्यकता है।

स्पष्ट रूप से अक्षम लेआउट और दृश्य रेंडरर:

public function getJsonResponseAction() 
{ 
    $this->getHelper('Layout') 
     ->disableLayout(); 

    $this->getHelper('ViewRenderer') 
     ->setNoRender(); 

    $this->getResponse() 
     ->setHeader('Content-Type', 'application/json'); 

    // should the content type should be UTF-8? 
    // $this->getResponse() 
    //  ->setHeader('Content-Type', 'application/json; charset=UTF-8'); 

    // ECHO JSON HERE 

    return; 
} 

अपने json नियंत्रक कार्रवाई सहायक आप कार्रवाई करने के लिए एक json संदर्भ जोड़ने की जरूरत का उपयोग कर सकते हैं। इस मामले में जेसन हेल्पर लेआउट को अक्षम कर देगा और आपके लिए रेंडरर देखेंगे।

public function init() 
{ 
    $this->_helper->contextSwitch() 
     ->addActionContext('getJsonResponse', array('json')) 
     ->initContext(); 
} 

public function getJsonResponseAction() 
{ 
    $jsonData = ''; // your json response 

    return $this->_helper->json->sendJson($jsonData); 
} 
+0

वेणु की विधि बेहतर है! – Naelyth

0

यह बहुत आसान है।

public function init() 
{ 
    parent::init(); 
    $this->_helper->contextSwitch() 
     ->addActionContext('foo', 'json') 
     ->initContext('json'); 
} 

public function fooAction() 
{ 
    $this->view->foo = 'bar'; 
}