I want JSON response from cakePHP which i will render using backbone.js. But instead of JSON response i am getting default.ctp content also along with JSON response i dont't know why. Is there something which i can do not to include default.ctp content in JSON response?
here is my code to fetch JSON
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, max-age=0, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
//header('Content-type: text/json');
header('Content-type: application/json');
header('Pragma: no-cache');
//header("X-JSON: ".$content_for_layout);
$response['status'] = $status;
//$response['data']['titleForLayout'] = $title_for_layout;
$response['data']['validationErrors'] = $this->validationErrors;
$response['data']['sessionFlash'] = $this->Session->read('Message.flash.message');
//$response['data']['data'] = $this->data;
$response['data'][$this->request->params['controller']]['output'] = isset($output)?$output:null;
$output = json_encode($response);
if (isset($this->params['url']['callback'])) {
echo $this->params['url']['callback'] . '(' . $output . ');';
} else {
echo $output;
}
?>
where data->output contains the rows fetched.
Please help me out.
I get JSON response but the problem is get default.ctp content surrounding the response which i don't want. is there a way to do it?
Looks like you want Request Handling
http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html
You need to set the layout to be ajax or an empty layout.
In the controller set this:
$this->layout = 'ajax';
Related
I couldn't think of better keywords to Google this issue, so I apologize if this is a duplicate.
Here is my logout.pl script that basically erases cookie:
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI;
my $q = new CGI;
print $q->header('text/html');
my $cookie = $q->cookie(
-name => 'CGISESSID',
-value => '',
-expires => '-1d'
);
print $q->header(-cookie=>$cookie);
print $q->redirect('welcome.pl');
exit;
When I run this script in a browser, it prints the following:
Set-Cookie: CGISESSID=; path=/; expires=Mon, 17-Feb-2014 09:05:42 GMT Date: Tue, 18 Feb 2014 09:05:42 GMT Content-Type: text/html; charset=ISO-8859-1 Status: 302 Found Location: welcome.pl
What I want, however, is for the browser to delete the cookie and redirect to welcome.pl.
When you print $q->header, that prints all the headers, including the blank line which signals the end of headers, making anything after it content. You need to only print $q->header once, no more.
There is actually one more problem you might not figure out on your own. The “clear” cookie you’re trying to send to expire the session must be sent with the redirect. The -w switch is not usually what you want, just the use warnings you have too. Also, redirect URLs RFC:MUST be absolute. "welcome.pl" will in most likelihood work but it’s not a good practice and I had relative URIs bite very badly in a modperl app once. So, amended–
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use URI;
my $q = CGI->new;
my $cookie = $q->cookie(
-name => 'CGISESSID',
-value => '',
-expires => '-1d'
);
my $welcome = URI->new_abs("welcome.pl", $q->url);
print $q->redirect( -uri => $welcome,
-cookie => $cookie,
-status => 302 );
exit;
You should use $q->header only once in your script and that should be before using anything printable on page
However if I do return $response; all the data is perfectly displayed.
It seems like json_decode only works when I call the API via curl but that was when the API was in a separate app on another vhost. I'm re-integrating it into our main app but I'm unable to extract the data.
echo $response['error']
echo $response->error
return $response->error
none of the above works.
performing a foreach on $response, all I get is header info.
foreach ($response as $a)
{
echo $a;
}
returns
Cache-Control: no-cache Content-Type: application/json Date: Fri, 14 Feb 2014 09:24:02 GMT
my controller
$response = ApiHelper::getCcm($pid);
//convert raw json into array
$ja = json_decode($response,true);
var_dump($ja);
the apihelper
return Response::json(array(
'error' => false,
'data' => $data->toArray()
),200);
I wrote a small PERL script to fetch some data from an URL using PERL. Not being an experienced programmer, I used the examples I found here in Stackoverflow. However, I always get the response
{"error":{"code":2,"message":"post parameter request missing"}}
The script looks like this
my $uri = 'URL';
my $json = '{"sourceCountry":"DE","sourceStore":476,"targetCountry":"DE","targetStore":[869],"article":[110101]}';
my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $lwp = LWP::UserAgent->new;
$response = $lwp->request($req);
The complete response is this:
HTTP/1.1 200 OK
Connection: close
Date: Wed, 15 Jan 2014 14:29:06 GMT
Server: Apache
Content-Length: 63
Content-Type: application/json; charset=utf-8
Client-Date: Wed, 15 Jan 2014 14:29:06 GMT
Client-Peer: 10.200.10.74:80
Client-Response-Num: 1
X-Powered-By: PHP/5.3.3
{"error":{"code":2,"message":"post parameter request missing"}}
What did I wrong?
Try:
use HTTP::Request::Common 'POST';
my $lwp = LWP::UserAgent->new;
$lwp->request( POST $uri, 'Content-Type' => 'application/json', 'Content' => $json );
though that should do basically the same thing except also setting Content-Length.
Alternatively, if the error you mention is literally indicating there is supposed to be a POST parameter (aka form data parameter) named request, try:
use HTTP::Request::Common 'POST';
my $lwp = LWP::UserAgent->new;
$lwp->request( POST $uri, [ 'request' => $json ] );
Assuming that it is a JSON RPC 2 service that you are trying to contact, you are missing part of the structure.
my $json = '{
"jsonrpc":"2.0",
"id":1,
"method":"some-method",
"params":{"sourceCountry":"DE","sourceStore":476,"targetCountry":"DE","targetStore":[869],"article":[110101]}
}';
If it is a JSON RPC service you could use JSON::RPC::LWP; which combines LWP::UserAgent and JSON::RPC::Common. It uses Moose so there will be quite a few dependencies to install. ( It uses Moose because JSON::RPC::Common uses Moose. )
use JSON::RPC::LWP;
my $url = ...;
my $rpc = JSON::RPC::LWP->new(
agent => 'Example ',
);
my $response = $rpc->call(
$url, # uri
'some-method', # service
{
sourceCountry => "DE",
sourceStore => 476,
targetCountry => "DE",
targetStore => [869],
article => [110101],
} # JSON container
);
if( my $error = $response->error ){
print 'error: #', $error->code, ' "', $error->message, "\"\n";
}else{
print "success!\n";
use Data::Dumper;
print Dumper( $response->result );
}
Note that you don't give JSON::RPC::LWP a JSON string, you give it a data structure.
How to convert a html table content into an excel spreadsheet?
I got lot of codes there which are good in Chrome but not in Mozilla?
I need a browser compatible code for exporting html table content into spreadsheet.
You can use CSV format to export data to Excel which support CSV format.
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
Then use this export function
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
This is the way you can export information to CSV format. Which is easily to open in Excel.
I am uploading files(any type) in MySql tables's blob field. Now I am able to get binary data from that field and when I print it, it shows binary data in firbug console. But I want to download that file as it was uploaded.
How can I convert this binary data into orignal file? How to do it in zend?
Thanks
You need to set the headers at minimum you need
<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\";");
echo $data;
exit;
?>
Or preferablly
<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header ( "Content-Type: $filedatatype" );
header("Content-Disposition: attachment; filename=\"".$FileObj->name."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$filesize);
echo $data;
exit;
?>