0

I have this script that sends values in form of string to the server to change the ipv4 information on a device need some help automatically changing the URL to the new address upon success of the post request without using the origin as the Parent URL. like in the case of window.location.replace(myUrl);

I get http://192.168.10.128/192.168.10.125

Where 125 is the new address.

 $.ajax({
                type : 'POST',
                url : '/ipcfg/ipcfg_set.cgi',
                dataType : 'json',
                data : {
                    ipv4_addr : addr,
                    ipv4_mask : mask,
                    gw_addr : gw
                },
                success : function() {

                    var myUrl = encodeURI(addr);
                    window.location.replace(myUrl);
                    //document.location.href = myUrl;

                    //location.assign(href, myUrl);


                    console.log("sucesss");

                },
                error : function(xhr, type) {
                    console.log("failed" + type, +xhr);
                }

            });

        }
jackotonye
  • 3,537
  • 23
  • 31

1 Answers1

3

If you just pass 192.168.10.125 to location.replace, then that is pretty much the same as if you assigned f.e. index.html instead – it refers to a “file” (or folder) underneath the current path – and that is why http://192.168.10.128/192.168.10.125 is the only correct result here.

You need to prefix it with a protocol – same as if you were to link to a different website, www.google.com would not work as intended, you need to use http://www.google.com (or https://www.google.com or //www.google.com)

So if you want the browser to navigate to http://192.168.10.125, then that line in your code needs to be

window.location.replace("http://" + addr);

(Applying encodeURI in this scenario would be counter-productive.)


Edit, to address comment:

i want to know if I can redirect to the new URL without having to reload the page.

No, that is not possible.

And it should obvious, why not: If I could replace my domain https://myphishingsite.com, that I previously lured you onto via say, a cleverly worded e-mail, with https://yourrealonlinebankingsite.com in the address bar of your browser without reloading the current page, that could not possibly be good, right?

(The HTML5 History API allows you to replace part of what is displayed in the address bar, but only from the path component onwards to the right. The protocol, host name and port part of the URL must remain unchanged.)

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • I tried that but its more like an easy fix i want to know if I can redirect to the new URL without having to reload the page. – jackotonye Feb 04 '16 at 20:17