0

Consider the ajax request :

var idNumber = $("#Field_1").val();
var servletUrl = "someURL"; // some url goes here

$.ajax({ 
    url: servletUrl,
    type: 'POST',
    cache: false, 
    data: { }, 
    success: function(data){
        alert(data);
        window.location = "./replyToYou.html";
    }
    , error: function(jqXHR, textStatus, err){
        alert('text status '+textStatus+', err '+err + " " + JSON.stringify(jqXHR));
    }
});

How can I pass , on success idNumber to replyToYou.html , and how can I grab it in replyToYou.html ?

Much appreciated

JAN
  • 21,236
  • 66
  • 181
  • 318

2 Answers2

1

you have to different solution:

1- first is using queryString:

success: function(data){
    alert(data);
    window.location = "./replyToYou.html?idNumber=" + idNumber;
}

then read it from querystring using this function (I use it as a querystring helper which is originally created in this POST):

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
getParameterByName("idNumber");

2- the other option is using localStorage:

success: function(data){
    alert(data);
    localStorage.setItem("idNumber",idNumber);
    window.location = "./replyToYou.html";
}

and get it at the other page like:

localStorage.getItem("idNumber")
Community
  • 1
  • 1
Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
  • Correct me if I'm wrong , but using the `1st` method you suggested would cause the user to see in the address bar the details that are passed ? – JAN Feb 09 '14 at 06:09
  • @ron: that's right, but you can use Base64 or whatever, to encode it. I myself usually go with the second solution. – Mehran Hatami Feb 09 '14 at 06:12
  • Yes , I get something like this in the browser tab : `/register_replyToYou.html?idNumber=44444444444444444444` – JAN Feb 09 '14 at 06:27
  • @ron: then using the function I have mentioned, get it in your code like `alert(getParameterByName("idNumber"))`. – Mehran Hatami Feb 09 '14 at 06:29
  • @ron: let me know if it was helpful or not. – Mehran Hatami Feb 09 '14 at 07:18
  • Thanks a lot , first of all. Second , please pay attention that the `idnumber` is still presented on the browser tab . I guess I'll deal with it later ... – JAN Feb 09 '14 at 07:38
1

edit code js in success section :

success: function(data){
    alert(data);
   window.location.href = "./replyToYou.html?idNumber=" + idNumber;
}
evergreen
  • 7,771
  • 2
  • 17
  • 25