This is what I was able to get working...
I used a regular javascript "XMLHttpRequest()" rather than the jQuery
"|$.ajax({||});" from my original request.
|
General Requirements:
- Directory needs to be able to use an .htaccess file
- perl JSON Module needs to be installed
Suggested changes and modifications are greatly appreciated. I still
need to get GET, PATCH and DELETE working.
Thank You and I HOPE this comes in handy to others!!!
~Donavon
*.htaccess*
<Files "json-submit.epl">
EMBPERL_OPTIONS 256
</Files>
*base.html*
<!DOCTYPE html>
<html lang="en">
<head>
<title> </title>
</head>
<body >
Hello World!!!
<script>
var url = "json-submit.epl";
var data = {
"firstName" : "Joe1",
"lastName" : "Schmoe1"
};
var callback = function (data) {
var json = JSON.parse(data.responseText);
alert(JSON.stringify(json));
};
var request = new XMLHttpRequest();
request.open("POST", url);
request.onreadystatechange = function() {
if(request.readyState === 4 && callback) {
callback(request);
}
};
request.setRequestHeader('Content-Type', 'application/json;
charset=UTF-8');
request.send(JSON.stringify(data));
</script>
</body>
</html>
*json-submit.epl*
[-
use JSON; # imports encode_json, decode_json,
to_json and from_json.
# to mimic proper Boolean data type
use constant false => 0;
use constant true => 1;
%param = ();
$returnHash{method} = $req_rec->method();
# params from json
%param = ();
use CGI;
$q = new CGI;
if($req_rec->method() eq 'POST') {
$postData = decode_json($q->param('POSTDATA'));
%param = %$postData;
$return = true;
}
elsif($req_rec->method() eq 'PUT') {
$putData = decode_json($q->param('PUTDATA'));
%param = %$putData;
$return = true;
}
elsif($req_rec->method() eq 'GET') {
# ????????????????????????????????
$return = true;
}
elsif($req_rec->method() eq 'PATCH') {
# ????????????????????????????????
$return = true;
}
elsif($req_rec->method() eq 'DELETE') {
# ????????????????????????????????
$return = true;
}
else {
$return = false;
}
$returnHash{param} = $param;
%{$returnHash{param}} = %param;
$return = true;
$json{return} = $return;
$json{returnHash} = \%returnHash;
$escmode = 0;
print OUT encode_json(\%json);
-]
On 9/8/2016 10:04 AM, Donavon wrote:
I agree with all of the responses that Embed perl was made to mainly
use CGI.pm and multipart/form-data and really shouldn't be used
outside of that.
BUT As long as there is possibility that it might be able to receive
application/json data I have to look for a way to do it. It's part of
my job to look for solutions that work within current limitations.
If every response to this email is "It's not possible" that is
something I can take to a meeting. If the responses are "You
shouldn't or it's difficult" then I have to keep searching for a
solution that is understandable and workable.
I'm willing to pay a small consulting fee or a donation to a charity,
for assistance in figuring this out.
So what are the suggestions for handling a call like this on a script
by script basis. I might be possible to limit the scripts accepting
these calls to specific directories.
|varformData
={"data1":a,"data2":b,"data3":c};$.ajax({dataType:"json",contentType
:"application/json;
charset=UTF-8",url:'/folder1/doSomething1.epl',data
:formData,success:function(response){CurrentArray=response;}}); Thank
You, ~Donavon |
On 9/4/2016 2:30 AM, Michael Smith wrote:
I wouldn't use embperl to accept json. I see embperl as being
designed for HTML output, so I don't really see how it helps when
dealing with json.
On Fri, Sep 2, 2016 at 11:43 PM, Donavon <d...@mycopanet.com
<mailto:d...@mycopanet.com>> wrote:
Good to know that there are still some Embperl users out there.
We have a large SAAS system mainly in Embperl, so our usage of
Embperl is not going away anytime soon.
My current question to the Embperl community is:
*How to accept **application/json?*
**I know that "Embperl uses CGI.pm internally to process forms
encoded with multipart/form-data" and that get processed into %fdat.
Is there a way to disable this behavior dynamically per page to
accept application/json data.
Thanks,
~Donavon