Link to home
Start Free TrialLog in
Avatar of trevor1940
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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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
What did the console show when you did this? Chances are it was a CORS cross origin violation?

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?
Avatar of trevor1940
trevor1940

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
This generally means a sintax error in the back end perl script
Then I would fix your backend script - you can't get the AJAX working when there are errors in your PERL script
Yes I know but as stated the script works when run in a separate tab
Hi
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");
            },
        });

Open in new window


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.
It seems like your backend script failed to validate the list(into array)
I posted ajax script

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>";
}

Open in new window


Run this in a browser tab i get

 URL in browser ..../cgi-bin/ backEndScript.pl?list=123,456,789
print
ist[123,456,789]

Open in new window


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_field');
post and opened in a new tab
which suggest the ajax is wrong??
AJAX 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.

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.
Could this be due to the version of jquery?

I'm using jquery-1.6.2.js
Ajax from

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");
            },
        });

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DOH! #faceslap

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)
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
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.