I'm using a JavaScript plugin to get the IP address. If the IP address starts with 10.15 I want to assign one value to a variable, and if the IP starts with 10.13 I want to assign a different value to the same variable.(I don't know if the variable has to be in Perl or JavaScript)
I'm trying this but is not working.
my $propt = "";
getUserIP(function(ip) {
console.log('IP: ' + ip);
const ips = ip.split('.');
var pro = document.getElementById('property');
console.log(pro);
if (ips[0] === "10" && ips[1] === "15") {
pro.value = "propt1";
To_Here
$propt = "SRC";
print <<"To_Here";
}
else if(ips[0] === "10" && ips[1] === "13") {
pro.value = "propt2";
To_Here
$propt = "ACC";
print <<"To_Here";
}
else {
pro.value = "propt";
To_Here
$propt = "TAP";
print <<"To_Here";
}
console.log(pro);
First I tried passing the value to an HTML input and reading the value of the input but I don't know if this is possible in Perl
<input id="property" type="hidden" name="property" value=""/>
The final step of what I'm trying to do is to run a query based on the property
$ql = "Select from properties where property = '?????' <----
On the JavaScript side you will need to send a GET request to the Perl script. I've only done this using jQuery (see docs) so you'll need to adapt this if you want a pure JavaScript solution:
function getUserIp(ips) {
var ipString = ips.join(';');
$.ajax({
type: 'GET',
url: '/path/to/script.pl',
data: { user_ips : ipString },
statusCode: {
200: function(data, textStatus, jqXHR) {
$('#id').html(jqXHR.responseText);
}
}
});
}
Note that the ips variable should be a string when you pass it to Perl. You can pass an array of params to Perl (see CGI docs), but I've found splitting a string after the fact to be the most reliable.
I'll show how to capture the parameter using Perl CGI because its simple, but if you're planning on making a full website then I strongly recommend using a web framework. There are several for Perl, like Catalyst and Mojolicious, with varying learning curves.
Using Perl's CGI module, you can capture parameters using the aptly-named param() method:
#! perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # just to make it easier to see errors
my $cgi = CGI->new;
my $ip_string = $cgi->param('user_ips');
my #ips = split(';', $ip_string);
my $results;
foreach my $ip (#ips) {
# do whatever here to populate $results
}
# send results back to jQuery
print $cgi->header( -type => 'text/plain', -status => '200' );
print $results;
print $cgi->end_html;
1;
Make sure you add the appropriate header that corresponds to the statusCode in the $.ajax() method and also call end_html() at the end of the Perl script, otherwise the jQuery/JavaScript may not understand/capture the results.
Related
I am using a Laravel blade.
My code below happens error when the size of item of textarea is huge.
I am looking for a way to solve it.
Everyone. How should I send a huge text to server?
How should I modify my codes?
Blade(JavaScript)
function sendByGet()
{
var items = document.getElementById("item").value;
var param = "?items="+items+"&id={{$id}}";
var url = {{(url)}} + encodeURI(param);
let result = fetch(url);
result.then(response => response.json()).then(
responceObject =>{
}
}
}
Controller PHP
public function receivebyGet(Request $request)
{
$lineArray = array();
$slipData = explode("\n", $request->items);
Error
the date is replaces <huge_text> (Actual data is text(5000 characters))
phpbladeName?id=01:270 GET https://test.com/test/send_by_get?&item=<huge_text> 414
sendByGet # confirmation?id=01:270
onclick # confirmation?id=0:244
VM142:1 Uncaught (in promise) SyntaxError: Unexpected end of JSON input
at phpbladeName?id=01
Move your data to the BODY
function sendByGet(url) {
const items = document.getElementById("item").value;
const param = encodeURI("?id={{$id}}");
fetch(url + param, {
method: 'GET',
headers: { 'Content-Type': 'plain/text' },
body: items,
})
.then(response => response.json())
.then( ... );
}
PHP Controller (assuming being Laravel)
public function receivebyGet(Request $request) {
$lineArray = array();
$slipData = explode("\n", $request->getContent());
...
}
Query size limit
As mentioned by Peter Krebs the maximum URL size (which includes the Query) is 2000 characters. So you cannot expect your system to reliably work if url is longher than 2000. Read more here.
GET body
As pointed out by Jabaa you should semantically choose the method, not technically. But this is something that has evolved over time (initially the GET body was supposed to be rejected/ignored) read more here. Hence you should consider it carefully and verify that all the parties involved (server, browser, proxies, cache) supports it properly. This is why oftentimes developers choose to violate semantics and use a POST method.
I'm using mirth connect 3.7, java version 1.8. i am new to both mirth and javascript. I have set up a channel destination to a javascript writer to get data out of xml files inserted into a mysql db. a sample section of xml file as follows:
...
<DG1>
<DG1.1>
<DG1.1.1>1</DG1.1.1>
</DG1.1>
<DG1.2>
<DG1.2.1>I10</DG1.2.1>
</DG1.2>
<DG1.3>
<DG1.3.1>R10.9</DG1.3.1>
</DG1.3>
<DG1.4>
<DG1.4.1>UNSPECIFIED ABDOMINAL PAIN</DG1.4.1>
</DG1.4>
<DG1.5/>
<DG1.6>
<DG1.6.1>A</DG1.6.1>
</DG1.6>
<DG1.7/>
<DG1.8>
<DG1.8.1>391</DG1.8.1>
</DG1.8>
<DG1.9/>
<DG1.10/>
<DG1.11>
<DG1.11.1>4252.21</DG1.11.1>
</DG1.11>
<DG1.12/>
<DG1.13/>
<DG1.14/>
<DG1.15/>
<DG1.16/>
<DG1.17/>
<DG1.18>
<DG1.18.1>N</DG1.18.1>
</DG1.18>
</DG1>
<DG1>
<DG1.1>
<DG1.1.1>2</DG1.1.1>
</DG1.1>
<DG1.2>
<DG1.2.1>I10</DG1.2.1>
</DG1.2>
<DG1.3>
<DG1.3.1>R10.9</DG1.3.1>
</DG1.3>
<DG1.4>
<DG1.4.1>UNSPECIFIED ABDOMINAL PAIN</DG1.4.1>
</DG1.4>
<DG1.5/>
<DG1.6>
<DG1.6.1>A</DG1.6.1>
</DG1.6>
<DG1.7/>
<DG1.8>
<DG1.8.1>391</DG1.8.1>
</DG1.8>
<DG1.9/>
<DG1.10/>
<DG1.11>
<DG1.11.1>4252.21</DG1.11.1>
</DG1.11>
<DG1.12/>
<DG1.13/>
<DG1.14/>
<DG1.15/>
<DG1.16/>
<DG1.17/>
<DG1.18>
<DG1.18.1>N</DG1.18.1>
</DG1.18>
</DG1>
...
I am trying to get the datapoints out of the xml iteratively so i can insert these diagnosis codes in a mysql table. my script at this point:
try {
var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('com.mysql.jdbc.Driver', 'jdbc:mysql://127.0.0.1:3306/adt', 'xxx', 'xxx');
var xml = new XML(connectorMessage.getEncodedData());
var myNodeList = xml.querySelectorAll("DG1");
for (i = 0; i < myNodelist.length; i++) {
var myQuery = 'INSERT INTO adt.diagnosis (AcctNum, MRN, ICD10) VALUES ("'+ $('AcctNum') + '", "' + $('MedRecNum') + '", "' + myNodelist[i]['DG1.3']['DG1.3.1'] + '")';
//do something with myVar to get a query...
dbConn.executeUpdate(myQuery);
}
} catch (ex) {
//handle any exceptions...
}
it runs without exceptions but i am not capturing the intended data obviously. Again, new to javascript, mirth and parsing xml. Questions:
obviously, i'm referencing the data points inappropriately, what is the nomenclature in javascript?
is there a dev environment in mirth that i can step through code and better troubleshoot?
are there any good recommended resources for javascript and xml as it pertains to mirth?
Mirth uses Mozilla Rhino for its Javascript engine. Rhino uses a deprecated standard called e4x for XML processing. If you search Google for e4x you'll find several pages at developer.mozilla.org with scary "obsolete" banners everywhere that can be helpful. The mirth user guide is very detailed when it comes to workflow within mirth.
https://github.com/mozilla/rhino
https://web.archive.org/web/20181120184304/https://wso2.com/project/mashup/0.2/docs/e4xquickstart.html (another good e4x resource)
https://www.nextgen.com/products-and-services/NextGen-Connect-Integration-Engine-Downloads (for the user guide)
I'm surprised querySelectorAll wasn't throwing an error. With minimal changes to your code:
try {
var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('com.mysql.jdbc.Driver', 'jdbc:mysql://127.0.0.1:3306/adt', 'xxx', 'xxx');
var xml = new XML(connectorMessage.getEncodedData());
// This should work, but is not typically how hl7 segments are accessed. Would need to see more than a segment for typical usage.
var myNodeList = xml.descendants("DG1"); // returns type XMLList
// length is a function instead of property on XMLList objects
for (i = 0; i < myNodelist.length(); i++) {
var myQuery = 'INSERT INTO adt.diagnosis (AcctNum, MRN, ICD10) VALUES ("'+ $('AcctNum') + '", "' + $('MedRecNum') + '", "' + myNodelist[i]['DG1.3']['DG1.3.1'] + '")';
dbConn.executeUpdate(myQuery);
}
} catch (ex) {
//handle any exceptions...
}
Using a for each loop and parameterized sql statement:
try {
var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('com.mysql.jdbc.Driver', 'jdbc:mysql://127.0.0.1:3306/adt', 'xxx', 'xxx');
var xml = new XML(connectorMessage.getEncodedData());
var myQuery = 'INSERT INTO adt.diagnosis (AcctNum, MRN, ICD10) VALUES (?, ?, ?)';
for each (var dg1 in xml.descendants('DG1')) {
dbConn.executeUpdate(myQuery, new java.util.ArrayList([$('AcctNum'), $('MedRecNum'), dg1['DG1.3']['DG1.3.1'].toString()]));
}
} catch (ex) {
//handle any exceptions...
}
You'll want a finally block after your try to close your database connection. If you remove the catch block, mirth will automatically set the message status to ERROR, write the exception to the server log, and fire an event which you can act on with a defined alert. That's usually easier than trying to handle the exception yourself.
Hi this is not quite right.
First ensure your data type is HL7.
Then do this (this is for insurance but you get the idea)
for each ( in1 in msg['IN1']) {
var effdate = in1['IN1.12']['IN1.12.1'];
// etc
}
I'd like to call a Python function from JavaScript code, because there isn't an alternative in JavaScript for doing what I want. Is this possible? Could you adjust the below snippet to work?
JavaScript code:
var tag = document.getElementsByTagName("p")[0];
text = tag.innerHTML;
// Here I would like to call the Python interpreter with Python function
arrOfStrings = openSomehowPythonInterpreter("~/pythoncode.py", "processParagraph(text)");
~/pythoncode.py contains functions using advanced libraries that don't have an easy to write equivalent in JavaScript:
import nltk # is not in JavaScript
def processParagraph(text):
...
nltk calls
...
return lst # returns a list of strings (will be converted to JavaScript array)
All you need is to make an ajax request to your pythoncode.
You can do this with jquery http://api.jquery.com/jQuery.ajax/, or use just javascript
$.ajax({
type: "POST",
url: "~/pythoncode.py",
data: { param: text}
}).done(function( o ) {
// do something
});
From the document.getElementsByTagName I guess you are running the javascript in a browser.
The traditional way to expose functionality to javascript running in the browser is calling a remote URL using AJAX. The X in AJAX is for XML, but nowadays everybody uses JSON instead of XML.
For example, using jQuery you can do something like:
$.getJSON('http://example.com/your/webservice?param1=x¶m2=y',
function(data, textStatus, jqXHR) {
alert(data);
}
)
You will need to implement a python webservice on the server side. For simple webservices I like to use Flask.
A typical implementation looks like:
#app.route("/your/webservice")
def my_webservice():
return jsonify(result=some_function(**request.args))
You can run IronPython (kind of Python.Net) in the browser with silverlight, but I don't know if NLTK is available for IronPython.
Communicating through processes
Example:
Python: This python code block should return random temperatures.
# sensor.py
import random, time
while True:
time.sleep(random.random() * 5) # wait 0 to 5 seconds
temperature = (random.random() * 20) - 5 # -5 to 15
print(temperature, flush=True, end='')
Javascript (Nodejs): Here we will need to spawn a new child process to run our python code and then get the printed output.
// temperature-listener.js
const { spawn } = require('child_process');
const temperatures = []; // Store readings
const sensor = spawn('python', ['sensor.py']);
sensor.stdout.on('data', function(data) {
// convert Buffer object to Float
temperatures.push(parseFloat(data));
console.log(temperatures);
});
Typically you would accomplish this using an ajax request that looks like
var xhr = new XMLHttpRequest();
xhr.open("GET", "pythoncode.py?text=" + text, true);
xhr.responseType = "JSON";
xhr.onload = function(e) {
var arrOfStrings = JSON.parse(xhr.response);
}
xhr.send();
You cannot run .py files from JavaScript without the Python program like you cannot open .txt files without a text editor. But the whole thing becomes a breath with a help of a Web API Server (IIS in the example below).
Install python and create a sample file test.py
import sys
# print sys.argv[0] prints test.py
# print sys.argv[1] prints your_var_1
def hello():
print "Hi" + " " + sys.argv[1]
if __name__ == "__main__":
hello()
Create a method in your Web API Server
[HttpGet]
public string SayHi(string id)
{
string fileName = HostingEnvironment.MapPath("~/Pyphon") + "\\" + "test.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo(#"C:\Python27\python.exe", fileName + " " + id)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
return p.StandardOutput.ReadToEnd();
}
And now for your JavaScript:
function processSayingHi() {
var your_param = 'abc';
$.ajax({
url: '/api/your_controller_name/SayHi/' + your_param,
type: 'GET',
success: function (response) {
console.log(response);
},
error: function (error) {
console.log(error);
}
});
}
Remember that your .py file won't run on your user's computer, but instead on the server.
Despite what some replies and comments suggest, there are a number of ways for using Python on the front-end. For your question in particular, see this reply.
I am using Python cherrypy and Jinja to serve my web pages. I have two Python files: Main.py (handle web pages) and search.py (server-side functions).
I create a dynamic dropdown list (using JavaScript) based on a local JSON file called component.json(created by function componentSelectBar inside search.py).
I want to ask how can my JavaScript retrieve JSON data without physically storing the JSON data into my local website root's folder and still fulfil the function of dynamic dropdown list.
The componentSelectBar function inside search.py:
def componentSelectBar(self, brand, category):
args = [brand, category]
self.myCursor.callproc('findComponent', args)
for result in self.myCursor.stored_results():
component = result.fetchall()
if (len(component) == 0):
print "component not found"
return "no"
components = []
for com in component:
t = unicodedata.normalize('NFKD', com[0]).encode('ascii', 'ignore')
components.append(t)
j = json.dumps(components)
rowarraysFile = 'public/json/component.json'
f = open(rowarraysFile, 'w')
print >> f, j
print "finish component bar"
return "ok"
The selectBar.js:
$.getJSON("static/json/component.json", function (result) {
console.log("retrieve component list");
console.log("where am i");
$.each(result, function (i, word) {
$("#component").append("<option>"+word+"</option>");
});
});
store results from componentSelectBar into database
expose new api to get results from database and return json to browser
demo here:
#cherrypy.expose
def codeSearch(self, modelNumber, category, brand):
...
result = self.search.componentSelectBar(cherrypy.session['brand'], cherrypy.session['category'])
# here store result into a database, for example, brand_category_search_result
...
#cherrypy.expose
#cherrypy.tools.json_out()
def getSearchResult(self, category, brand):
# load json from that database, here is brand_category_search_result
a_json = loadSearchResult(category, brand)
return a_json
document on CherryPy, hope helps:
Encoding response
In your broswer, you need to GET /getSearchResult for json:
$.getJSON("/getSearchResult/<arguments here>", function (result) {
console.log("retrieve component list");
console.log("where am i");
$.each(result, function (i, word) {
$("#component").append("<option>"+word+"</option>");
});
});
To use that json data directly into javascript you can use
var response = JSON.parse(component);
console.log(component); //prints
OR
You already created json file.If that file is in right format then you can read json data from that file using jQuery jQuery.getJSON() For more: http://api.jquery.com/jQuery.getJSON/
You are rendering a HTML and sending it as response. If you wish to do with JSON, this has to change. You should return JSON in your main.py, whereas you will send a HTML(GET or POST) from Javascript and render it back.
def componentSelectBar(self, brand, category):
/* Your code goes here */
j = json.dumps(components)
// Code to add a persistent store here
rowarraysFile = 'public/json/component.json'
f = open(rowarraysFile, 'w')
print >> f, j
// Better to use append mode and append the contents to the file in python
return j //Instead of string ok
#cherrypy.expose
def codeSearch(self):
json_request = cherrypy.request.body.read()
import json # This should go to the top of the file
input_dict = json.loads(json_request)
modelNumber = input_dict.get("modelNumber", "")
category = input_dict.get("category", "")
brand = input_dict.get("brand", "")
/* Your code goes here */
json_response = self.search.componentSelectBar(cherrypy.session['brand'], cherrypy.session['category'])
return json_response
Here, I added only for the successful scenario. However, you should manage the failure scenarios(a JSON error response that could give as much detail as possible) in the componentSelectBar function. That will help you keep the codeSearch function as plain as possible and help in a long run(read maintaining the code).
And I would suggest you to read PEP 8 and apply it to the code as it is kind of norm for all python programmers and help any one else who touches your code.
EDIT: This is a sample javascript function that will make a post request and get the JSON response:
searchResponse: function(){
$.ajax({
url: 'http://localhost:8080/codeSearch', // Add your URL here
data: {"brand" : "Levis", "category" : "pants"}
async: False,
success: function(search_response) {
response_json = JSON.parse(search_response)
alert(response_json)
// Do what you have to do here;
// In this specific case, you have to generate table or any structure based on the response received
}
})
}
i'm trying to develop Firefox extension
problem :
var Request = require("sdk/request").Request;
var latestTweetRequest = Request({
url: "file.php",
onComplete: function (response) {
var List = response.json;
}
});
I want to use this request function to parse json to an array (List here) from php file.
The php my php file echo json form correctly, but I can't transform the data into javascript array to be able to use it in my addon.
if there is a better idea than using this function to do it please tell me :)
try this: MDN - JSON Object
JSON.parse and JSON.stringify
var Request = require("sdk/request").Request;
var latestTweetRequest = Request({
url: "file.php",
onComplete: function (response) {
var List = JSON.parse(response.json);
}
});
it's very important to use double quotes.
If you are having a problem with JSON.parse. Copy your array to scratchpad and then run JSON.stringify on it and then make sure your php file matches the strignified result.
if Addon-SDK doesnt have JSON then you gotta require the module if there is one. If there isn't one than require('chrome') and grab the component HERE
There's a bug in Noitidarts code.
why JSON.parse the request.json? If you want to parse do it on request.text
However no need to json.parse as the request module tries to parse and if successful retuns request.json
see here:
var Request = require("sdk/request").Request;
var latestTweetRequest = Request({
url: "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=mozhacks&count=1",
onComplete: function (response) {
var tweet = response.json[0];
console.log("User: " + tweet.user.screen_name);
console.log("Tweet: " + tweet.text);
}
});
// Be a good consumer and check for rate limiting before doing more.
Request({
url: "http://api.twitter.com/1/account/rate_limit_status.json",
onComplete: function (response) {
if (response.json.remaining_hits) {
latestTweetRequest.get();
} else {
console.log("You have been rate limited!");
}
}
}).get();
so the likely problem is that your php is not outputting a json string that json.parse can read. make sure to use ". figure out what your php file should return by running json.stringify on a dummy object. ie:
var obj = {myarr:[1,8,9,7,89,0,'ji'],strr:'khhkjh',anothrtObj:{1:45,56:8}};
alert(JSON.stringify(obj)) //{"myarr":[1,8,9,7,89,0,"ji"],"strr":"khhkjh","anothrtObj":{"1":45,"56":8}}
so now in your php make sure your outputted text mateches this format
{"myarr":[1,8,9,7,89,0,"ji"],"strr":"khhkjh","anothrtObj":{"1":45,"56":8}}
if your php outputs something like below JSON.parse will fail on it so request.json will be null
{myarr:[1,8,9,7,89,0,"ji"],strr:"khhkjh",anothrtObj:{"1":45,"56":8}}
or
{'myarr':[1,8,9,7,89,0,"ji"],'strr':"khhkjh",'anothrtObj':{"1":45,"56":8}}
or
{'myarr':[1,8,9,7,89,0,'ji'],'strr':'khhkjh','anothrtObj':{'1':45,'56':8}}