trevor1940
asked on
JavaScript: Save function
Hi
I have an intranet web page that performs a search function situated on a fedora stand alone pc
I need to pass a unique comma separated list to an external script this script will use the PostGreSQL/ PostGis function "pgsql2shp"
I tried to use simple copy & Paste, from a TextArea on the web page /run external script & paste into a terminal window but the paste buffer truncates the list
Next I've tried to send the list to the server using jQuery\AJAX and save the file this also Fails I'm guessing due to security however if I run the receiving back-end script in its own tab it works
I'm using jQuery to loop through the ticked checkboxes to push the list into an array and outputting to a TextArea
How might
ensure the javascript array is unique
second create a save button that saves the value of the text area to a file at a fixed location over writing what is there
I have an intranet web page that performs a search function situated on a fedora stand alone pc
I need to pass a unique comma separated list to an external script this script will use the PostGreSQL/ PostGis function "pgsql2shp"
I tried to use simple copy & Paste, from a TextArea on the web page /run external script & paste into a terminal window but the paste buffer truncates the list
Next I've tried to send the list to the server using jQuery\AJAX and save the file this also Fails I'm guessing due to security however if I run the receiving back-end script in its own tab it works
I'm using jQuery to loop through the ticked checkboxes to push the list into an array and outputting to a TextArea
How might
ensure the javascript array is unique
second create a save button that saves the value of the text area to a file at a fixed location over writing what is there
ASKER
Hi
The console shows NetworkError: 500 Internal Server Error
This generally means a sintax error in the back end perl script
The TexArea isn't strictly necessary I just need the Comma seperated list generated from Checbox(s) attributes eg data-mydata
The server is Apache running on Fedora 12
The console shows NetworkError: 500 Internal Server Error
This generally means a sintax error in the back end perl script
The TexArea isn't strictly necessary I just need the Comma seperated list generated from Checbox(s) attributes eg data-mydata
The server is Apache running on Fedora 12
This generally means a sintax error in the back end perl scriptThen I would fix your backend script - you can't get the AJAX working when there are errors in your PERL script
ASKER
Yes I know but as stated the script works when run in a separate tab
ASKER
Hi
Further digging Think it could be the jquery \ ajax function
Looking at Firebug console Params are correct
Further digging Think it could be the jquery \ ajax function
var url = "backEndScript.pl?list=123,456,789,";
$ajax({
url: url,
type:"post",
dataType:"text",
success: function(data, textStatus, jQxHr) {
alert("Data was succesfully captured");
},
});
Looking at Firebug console Params are correct
IF it is a perl script - it does not run in a Tab it runs on the server.
If it is working on one URL and not through AJAX then the obvious reason is that what you are sending it via AJAX is causing it to choke - and you have to look at that.
In your console (console tab) examine the request (GET / PUT) and see what is in the parameters tab and the response tab.
Compare that to the separate tab that works.
We can't even start to move forward until we resolve that 500 server error.
Is your PERL script expecting a GET or a POST?
What parameters does it expect?
What does your AJAX code look like?
Have you enabled CORS on the server?
Let's start there.
If it is working on one URL and not through AJAX then the obvious reason is that what you are sending it via AJAX is causing it to choke - and you have to look at that.
In your console (console tab) examine the request (GET / PUT) and see what is in the parameters tab and the response tab.
Compare that to the separate tab that works.
We can't even start to move forward until we resolve that 500 server error.
Is your PERL script expecting a GET or a POST?
What parameters does it expect?
What does your AJAX code look like?
Have you enabled CORS on the server?
Let's start there.
It seems like your backend script failed to validate the list(into array)
ASKER
I posted ajax script
perl script using tried and tested catch all
Run this in a browser tab i get
the URL was taken from firebug console -> post and opened in a new tab
which suggest the ajax is wrong??
perl script using tried and tested catch all
use CGI;
my $q = CGI->new;
my @Names = $q->param;
foreach my $name (@Names){
print "<p>" . $name . '['. $q->param{$name} . "]</p>";
}
Run this in a browser tab i get
URL in browser ..../cgi-bin/ backEndScript.pl?list=123,456,789
print
ist[123,456,789]
the URL was taken from firebug console -> post and opened in a new tab
which suggest the ajax is wrong??
Could it be? CGI?
my @values = $q->multi_param('form_fiel d');
my @values = $q->multi_param('form_fiel
post and opened in a new tabAJAX is identical to using the browser tab. The only difference is that in the case of the target url being on a different domain / protocol or port, the browser sends a pre-flight (options) request to the server to find out whether it supports cross domain requests.
which suggest the ajax is wrong??
Apart from that the calls are identical.
If the AJAX fails and the browser tab works - then they are not the same URL - did you check the parameters tab?
You have not posted your AJAX code - which we need to see.
ASKER
Could this be due to the version of jquery?
I'm using jquery-1.6.2.js
I'm using jquery-1.6.2.js
ASKER
Ajax from
https://www.experts-exchange.com/questions/29136585/JavaScript-Save-function.html?anchorAnswerId=42803304#a42803304
https://www.experts-exchange.com/questions/29136585/JavaScript-Save-function.html?anchorAnswerId=42803304#a42803304
var url = "backEndScript.pl?list=123,456,789,";
$ajax({
url: url,
type:"post",
dataType:"text",
success: function(data, textStatus, jQxHr) {
alert("Data was succesfully captured");
},
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
DOH! #faceslap
Thank you
Thank you
You are welcome
BTW I would upgrade your jQuery. 1.x is at 1.12 - it is not going to be updated further and there is a known vulnerability with it.
jQuery 3.x is the current standard version (potentially use with jQuery Migrate if there are backward compatibility issues)
jQuery 3.x is the current standard version (potentially use with jQuery Migrate if there are backward compatibility issues)
ASKER
Yes this box reached it's EOL ages ago and should be replaced with a total revamp
Now getting a HTTP 414 “Request URI too long” error so might have to go down the Save file option from the browser
Now getting a HTTP 414 “Request URI too long” error so might have to go down the Save file option from the browser
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/414
When you convert a POST to a GET - any data that was being posted in the Body of the request now goes to the URL.
URL's have a length limit of 2K - so if your data that you are sending is more than this then you will have a problem.
The correct method is to use a POST as your AJAX was originally set to. To make it work however you will need to convert your PERL script to accept a POST request rather than a GET.
When you convert a POST to a GET - any data that was being posted in the Body of the request now goes to the URL.
URL's have a length limit of 2K - so if your data that you are sending is more than this then you will have a problem.
The correct method is to use a POST as your AJAX was originally set to. To make it work however you will need to convert your PERL script to accept a POST request rather than a GET.
What is it you are wanting to do?
Is it save a text box to a file OR save checkbox values to a file OR both.
If you want to do this by AJAX then you must enable CORS to allow your AJAX script to talk to your server - but we would need to know what your server environment looks like - what scripting environment are you running?