2

Newbie question.

Found that most of tutorials in the Internet focused how to pass parameters by GET / POST. Just some of them pointed to retrieval data and mostly by using PHP (jQuery docs) / ASP.NET etc.

How to retrieve AJAX posted data using pure JavaScript?

Exactly:

Post:

function detailOperator(_recordId, _title) {
   $.mobile.changePage('#operator-view', 
     { dataUrl: '?ID=' + _recordId + '&title=' + _title});
}

Post changing page successfully.

How to retrieve ID and title in operator-view page?

Valery Bulash
  • 445
  • 4
  • 22
  • 1
    Go to my other article: http://stackoverflow.com/a/14469041/1848600 and search chapter: Data/Parameters manipulation between page transitions. You will find several solutions to your need. – Gajotres Apr 18 '13 at 13:42

3 Answers3

4

POST data cannot be obtained by client side scripts, unless the server side script handing the POST request sends it back to the client with the response. In short, there is no built-in way of doing this.

However, your server side script can choose to pass the POST data back to the client via cookies or hidden variables and then your client side JavaScript can access the values therein.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
1

What finally I've got (commented Jim' idea):

Pass parameters:

function detailOperator(_recordId) {
    $.mobile.changePage('#operator-view', { dataUrl: '?ID=' + _recordId });
    // Below not working (no errors) - maybe this sample working for external pages?
    //$.mobile.changePage('#operator-view', { dataUrl : '?ID=' + _recordId, data : { 'ID' : _recordId }, reloadPage : true, changeHash : true });
}

so, here the same as it was.

Retrieve parameters:

// Below not working (no errors) because of empty "url"
/*
var parameters = $(this).data("url").split("?")[1];;
var _recordId = parameters.replace('ID=', '');
*/
var parameters = location.hash.substring(2).split("&");
var _recordId = parameters[0].replace('ID=', '');

Thanks a lot for patience!

Valery Bulash
  • 445
  • 4
  • 22
  • You probably should mark this then as your accepted answer, you can do so by ticking off the hollow check-mark to the left of this answer. – Jack Apr 18 '13 at 16:59
  • StackOverflow allows to accept own answer in 2 days - not earlier :-( – Valery Bulash Apr 18 '13 at 17:43
0

POST values are not accessible client side. GET values can be accessed via

window.location.search
Edd
  • 3,724
  • 3
  • 26
  • 33
Jim Ouwerkerk
  • 43
  • 1
  • 10
  • Thanks, Jim. But I found that `$('#operator-modify').live('pageshow', function(event, ui) { alert(location.search); });` show empty string. Does it mean wrong call of $.mobile.changePage ? – Valery Bulash Apr 18 '13 at 14:08
  • I never really used jquery mobile, i wanted to post my answer in a comment but i don't got enough rep for that. But as Gajotres says: Go to my other article: [http://stackoverflow.com/a/14469041/1848600](http://stackoverflow.com/a/14469041/1848600) and search chapter: Data/Parameters manipulation between page transitions. You will find several solutions to your need. – Jim Ouwerkerk Apr 18 '13 at 15:06
  • @user1895410 just a note as of jQuery 1.7 [.live](http://api.jquery.com/live/) has been deprecated in favor of [.on](http://api.jquery.com/on/) – Jack Apr 18 '13 at 16:07