HTML POST and GET from within JavaScript

S

SteveYoungTbird

I am using the following function to call a PHP script which then
returns its result to the callback javascript function "loadJSON". This,
which is really the HTML GET Method, works fine except when the points
variable is large and then obviously it doesn't work.

function getElevationIntermediate(points) {
var script = document.createElement('script');
document.body.appendChild(script);
script.src = 'http://' + geoSpokeAddressTest + '?spoke=' + points +
'&callback=loadJSON';
}

Therefore I would like to use the HTML POST method instead but I cannot
find out how to do this from within JavaScript. I have tried many ideas
including the following but the PHP script receives the variables but
doesn't use the callback.

HTML
<form id="postit" style="display:none" method="post" action="">
<input id="spoke">
<input id="callback">
<input type="submit">
</form>

JavaScript
var phpScript = "http://mysite/postex.php",
callback = "loadJSON";
document.getElementById('postit').action = phpScript;
document.getElementById('spoke').value = points;
document.getElementById('callback').value = callback;
document.getElementById('postit').submit();

PHP
$latlngs = $_POST["spoke"];
$callback = $_POST["callback"];
............................
$json1 = json_encode($heights);
echo "$callback($json1)";

Thanks in advance for any advice or pointers , Steve.
 
E

Eric Bednarz

SteveYoungTbird said:
I am using the following function to call a PHP script which then
returns its result to the callback javascript function
"loadJSON". This, which is really the HTML GET Method,

The HTTP GET method, really, but what’s more important, this
var script = document.createElement('script');

retrieves additional data, while this
document.getElementById('postit').submit();

navigates away from the current site.
You should probably start with explaining the problem you want to solve.
$callback = $_POST["callback"]; […]
echo "$callback($json1)";

Lose the quotes and read up on variable functions in the PHP
documentation. If you call functions with names that are provided by GET
or POST data, the least you should do is testing them with
function_exists.
 
S

SteveYoungTbird

Eric said:
The HTTP GET method, really, but what’s more important, this


retrieves additional data, while this


navigates away from the current site.
You should probably start with explaining the problem you want to solve.
The problem I want to solve is this: The PHP script gets height data
using lat/lng pairs in the points variable. The lat/lng pairs are 100m
apart. If the total distance is over about 16km the url is too big. What
I would like to do is to use HTTP POST instead of HTTP GET from within
JavaScript so that I can use a points array for links over 16km.
$callback = $_POST["callback"]; […]
echo "$callback($json1)";

Lose the quotes and read up on variable functions in the PHP
documentation. If you call functions with names that are provided by GET
or POST data, the least you should do is testing them with
function_exists.

The functions do exist, as I said this works until the points variable,
and therefore the URL, gets too big.

function getElevationIntermediate(points) {
var script = document.createElement('script');
document.body.appendChild(script);
script.src = 'http://' + geoSpokeAddressTest + '?spoke=' + points +
'&callback=loadJSON';
}



The second part of my OP is there to show that I have at least thought
about this, the scripts are probably rubbish. (Except for the PHP part
which is exactly the same as for the GET method except for the use of
$_POST instead of $_GET.) Feel free to ignore the JavaScript and HTML
parts if you can explain how to use the POST method from within
JavaScript with a better example.
 
J

Jorge

(...)
Thanks in advance for any advice or pointers , Steve.

Either send the form from a hidden iframe or, instead of inserting a
script, send a (POST) xmlHttpRequest, SOP permitting... (?), then eval
("("+ xhr.responseText +")");
 
S

SteveYoungTbird

Jorge said:
Either send the form from a hidden iframe or, instead of inserting a
script, send a (POST) xmlHttpRequest, SOP permitting... (?), then eval
("("+ xhr.responseText +")");

Thanks Jorge,

The SOP is the problem here otherwise I would have simply used
xmlHttpRequest as you suggest. I should have said that this was the
reason for using the <script> tags.

Could you possibly tell me how to send the form from a hidden iframe?

Regards,

Steve.
 
S

SteveYoungTbird

SteveYoungTbird said:
Thanks Jorge,

The SOP is the problem here otherwise I would have simply used
xmlHttpRequest as you suggest. I should have said that this was the
reason for using the <script> tags.

Could you possibly tell me how to send the form from a hidden iframe?

I have found some information on using hidden frames and will try it
out. I will post the solution if anyone is interested, assuming I find
one :).
Regards, Steve.
 
S

SteveYoungTbird

SteveYoungTbird said:
I have found some information on using hidden frames and will try it
out. I will post the solution if anyone is interested, assuming I find
one :).
Regards, Steve.

Unfortunately using hidden iframes doesn't seem to be a solution as the
traversing of the iframe is subject to the constraints of the
same-origin policy.

Does anyone have any other ideas of how one can get around the url size
limits of the script tag?

As a reminder the problem I want to solve is this:
I am using the script tag to call a PHP script which then returns its
result to the callback javascript function "loadJSON". This works fine
except when the "points" array variable, and therefore the url, is too
large. What method could I use for larger "points" arrays?

Perhaps I will have to split the "points" array and make two or more GET
requests.

Regards, Steve.

Regards, Steve.
 
J

Jorge

(...)
Unfortunately using hidden iframes doesn't seem to be a solution as the
traversing of the iframe is subject to the constraints of the
same-origin policy.

Yes, it is, but still, ISTM that you can call top.someFunction(myData)
from the iframe, isn't it ?
 
S

SteveYoungTbird

Jorge said:
Yes, it is, but still, ISTM that you can call top.someFunction(myData)
from the iframe, isn't it ?

I am not sure what you mean.

I think I will go for splitting the array and using two or more GET
requests unless someone can explain a better way.

Thanks for your response and time, Steve.
 
J

Jorge

I am not sure what you mean. (...)

That the .html that the <iframe> receives back in response to the
POSTed <form>, has to be something like this:

<html>
<head>
<script>
top.myCallBackFunction(theDataGoesHere);
</script>
</head>
<body>
</body>
</html>

myCallBackFunction is a global function in the window that contains
the <iframe>, that gets called from the <iframe> with the data as a
parameter:

function myCallBackFunction (theData) {
//use theData here.
}

I might be completely wrong, hehe. But I'd give it a try...
 
E

Eric Bednarz

SteveYoungTbird said:
Eric Bednarz wrote:
$callback = $_POST["callback"]; […]
echo "$callback($json1)";

Lose the quotes and read up on variable functions in the PHP
documentation. If you call functions with names that are provided by GET
or POST data, the least you should do is testing them with
function_exists.

The functions do exist,

If you use variable functions with strings provided by GET/POST data,
you do *not* know if those functions exists since anybody can request
the handler with any value for ‘callback’.
as I said this works until the points
variable, and therefore the URL, gets too big.

You also said the callback is not used. From your reply, I’m not even
sure if you understand what you are doing wrong in the above code.
Feel free to ignore the JavaScript and HTML
parts if you can explain how to use the POST method from within
JavaScript with a better example.

You can use XHR and bypass your SOP problems serve-side. If you have
apache and mod_rewrite available you can use the latter’s proxy
throughput feature (that’s a one-liner), and if you have PHP it’s fairly
easy to get a response message body from another server with PHP’s
libcurl support (if HTTP headers are important that’s a bit more work).
 
D

David Mark

The problem I want to solve is this: The PHP script gets height data
using lat/lng pairs in the points variable. The lat/lng pairs are 100m
apart. If the total distance is over about 16km the url is too big. What
I would like to do is to use HTTP POST instead of HTTP GET from within
JavaScript so that I can use a points array for links over 16km.
    $callback = $_POST["callback"];
[…]
    echo "$callback($json1)";
Lose the quotes and read up on variable functions in the PHP
documentation. If you call functions with names that are provided by GET
or POST data, the least you should do is testing them with
function_exists.

I tried removing the the quotes and for the $_POST variable it made no
difference but for the

echo "$callback($json1)";

the quotes are required. Remove the quotes and the JSON is not made
available to the calling script.

It seems you are just fumbling around in the dark. Both of you.
There was some sort of misunderstanding between Erich and myself,

Or perhaps between you and your alter ego?
probably my fault, because Erich thought that the callback never
worked at all but it worked fine with GET data until the URL got too
long and then nothing was returned. I assume that it reached the
Apache limit for URL length.

Who is Erich?
Anyway, I got around the problem by slicing the array into what seemed
to be acceptable lengths and making a GET request for each slice. This
works but the maximum URL length seems to be different for different
clients, by that I mean Firefox on different machines even with the
same OS. I need to find out why this is, does anyone know?

Why don't you ask in an appropriate group.
I would still like to use a POST request instead of a GET request if
possible.

Do you know the difference?
I will experiment some more with Jorge's suggestion when I
get time. I would also be grateful for any other suggestions.

Get a brain? Stop wasting time here.
I'm off
to Ireland tomorrow and then Russia so I won't get much time at my own
box.

Have a nice trip.
 
D

David Mark

On Jun 2, 8:40 am, SteveYoungGoogle <[email protected]>
wrote:
Eric Bednarz wrote:
I am using the following function to call a PHP script which then
returns its result to the callback javascript function
"loadJSON". This, which is really the HTML GET Method,
The HTTP GET method, really, but what’s more important, this
    var script = document.createElement('script');
retrieves additional data, while this
    document.getElementById('postit').submit();
navigates away from the current site.
You should probably start with explaining the problem you want tosolve.
The problem I want to solve is this: The PHP script gets height data
using lat/lng pairs in the points variable. The lat/lng pairs are 100m
apart. If the total distance is over about 16km the url is too big.What
I would like to do is to use HTTP POST instead of HTTP GET from within
JavaScript so that I can use a points array for links over 16km.
    $callback = $_POST["callback"];
[…]
    echo "$callback($json1)";
Lose the quotes and read up on variable functions in the PHP
documentation. If you call functions with names that are providedby GET
or POST data, the least you should do is testing them with
function_exists.
I tried removing the the quotes and for the $_POST variable it made no
difference but for the
echo "$callback($json1)";
the quotes are required. Remove the quotes and the JSON is not made
available to the calling script.
It seems you are just fumbling around in the dark.  Both of you.

You are welcome to your opinion.

You should take something from it.
Eric Bednarz. I apologize to Eric for the misspelling.



My question is "How can I use HTTP POST instead of HTTP GET from
within JavaScript so that I can use a points array for links which are
to long for including in the URL?" The result has to be JSON encoded
height data for inclusion on the same page, the same as happens with
GET. I think that this is the appropriate group to ask this question.

I don't.
Of course, with GET the data is included in the URL and there is a
limit to the URL lengthhttp://www.boutell.com/newfaq/misc/urllength.html
That is why I have asked if I could do this with POST within
javaScript.

That's what I thought. You don't know the difference.
I am happy to hear that you have finally got a job.

I've had the same job for nearly ten years now. And what possible
business is that of yours?

You don't help yourself, do you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top