Read Rewritten URL - javascript - javascript

I need to read the original URL from the rewritten path.
this is how i do url rewrite in my webapplication. I don't have codebehind in my application. So i read everything through javascript and call a webservice. While calling the webservice, i need to read query parameter ID and pass it.
Is my url rewrite method is right? if so, how can i read the original URL i.e. pages/products.aspx?ID=123. Because in my browser address bar it shows only product.aspx, i get the same through javascript.

Don't know whether right or wrong way, anyhow I managed to get the query string using JavaScript.
The query string was not visible in the browser window, but in the form tag it is available. So I get that using document.getElementsByTagName('form')[0].action, and completed my task.

Related

Get parameter from javascript url

how I can get parameters from my script url? I found examples where was parameters from website url but it's not what i really looking for. I'm running one script from another and I send request like /script.js?name=example and I don't have idea how I can get access to name in script.js.
Greetings.

How to get data from remote website?

Lets say there is a url out there e.g. www.website.com/data.jsp
the link has the following JSON data
{"successful":"true","rows":[{"zip":"65472","user_id":"10843","name":"Rufio"}]}
I just want to be able to extract this data at runtime however I am having a hard time getting it using getJSON
$.getJSON("test2.jsp",function(result){
$("div").append(result.rows[0].user_id + " ");
});
Now if I run it using a local file with the data residing in test2.jsp as shown above it appends the user_id. However when I try to access "www.website.com/data.jsp" instead nothing happens. I don't believe the website is configured to work with JSONP either.
I need a way to figure out how to pull this data from the website at run time. Does anyone have any solutions or workarounds?
p.s. Is this something that might need to be sorted out on the other end? The people who own the website set this scenario up to be like a fake api call like typically you would pass in parameters to get back the specific information that you would need. In the case of this endpoint or url it just returns a single record or the file just contains the data listed above. They would like me to extract the data from their url at runtime.
You can't make a normal ajax call to to this other domain due to same origin policy.
You can use JSONP to load the remote page, but looking at that example output you wouldn't be able to access the data unless the remote site is setup for JSONP (assigning the JSON to a variable, calling a callback function, etc).
You could create a server-side passthrough script of your own. You don't mention what server-side technology you have available, but if you can use PHP, you do a passthrough like this:
<?php
echo file_get_contents("http://www.website.com/data.jsp");
?>
PHP (or any other server-side language) can fetch the remote data, and now you can use ajax to call your own script (which works since you're on the same domain).

Is there a way I can retrieve the JSON file from server

Referring to my question above, the reason I want to retrieve the file is that I want to know how it is structured so I can make the script that I copied work using my data instead.
Currently, the $.getJSON url includes the string "callback=?" in it.
So is there anyway, I can peek through or see how the data is structured in the file thats inside the $.getJSON url string?
actually, Im following along a demo below
https://code.google.com/r/kgraham-flr-map/source/browse/examples/medicare-dynamic-ds.js?spec=svn72fd5de93cf24b0b2baa5d2678d9518741e3d80b&r=72fd5de93cf24b0b2baa5d2678d9518741e3d80b
Tthe script in question has the $.getJSON('https://storelocator-go-demo.appspot.com/query?callback=?' to parse the data.
When I tried opening the url, I get this error strconv.ParseFloat: parsing "": invalid syntax
I want to see how the JSON file is structured so I can replace it with my own data instead.
Hope this clears my question.
From the comments:
so you mean the error that I got when trying to open the json file is due to a server problem?
Well, it could give you a more useful error message. The problem is that you're not giving it the arguments it requires.
If I go to:
https://storelocator-go-demo.appspot.com/query?callback=foo
...then like you, I get that error. But if I give it arguments (these are cribbed from the examples link you gave):
https://storelocator-go-demo.appspot.com/query?callback=foo&lat=-29.292219923932738&lng=137.763512&n=-10.691329506145314&e=167.382652625&s=-47.89311034172016&w=108.14437137499999&audio=&access=&_=1364633286163
I get back a valid JSONP response.

sending data to another website and receive result

If i have a website1.com and website2.com, can I send data ( ex: value from input ) from website2.com to website1.com and receive result? with no page refresh or redirect. I'd like to use only javascript & ajax, no PHP or jQuery. If it is possible give me an example how to do it.
I thought about creating an script element ( with javascript) on website2.com with src like : website1.com?data=<value from input>, and when script element loads the src, it will show me an result, but maybe there is a better option to do this.
PS: I will have more separated datas to send.
Note: this is not XSS, just a public project for websites, which will need to update datas every x minutes and to send some data to website1.
Thanks.
Your question is not completely clear, but in general when you have to do cross-site AJAX you have to use JSONP
Since XmlHttpRequest does not work cross-domain, you have to use JSONP. Basically, this is adding a script tag dynamically as you're suggesting to do. Then, the server uses your GET datas, does whatever it wants, and usually "prints" a callback function.
When you call a file using the script tag, it will evaluate everything displayed. This is why, if, on the server side, you're doing :
<?php
echo 'alert(1);';
?>
This will be evaluated as javascript. You can then easily understand how to use a callback function (another GET parameter).
Also, in jQuery, there is an option called 'jsonp' when you call $.ajax (using "callback" as default GET parameter, but can be changed).
You could have some XSS issues but it is possible you could use ajaxgold. It is an pretty easy manner to send pretty much everything over.
Call send to website2
postDataReturnText( 'http://website2.com', 'data=bla', getResult );
Return the result
function getResult( text ) {}

When is a postback not a postback? (according to ASP.net)

Is there a difference between me using Javascript to redirect to URL + "?Querystring=value" versus using whatever mechanism ASP.NET uses?
If there is a difference, how can I make the rendered ASP.NET page be submitted to the same URL with a different query string by javascript?
If you want to do a post back just like a asp control like a asp:Button you can use the javascript functions included by the framework to do so:
__doPostBack('ControlIDOfEventYouWantToRaise','');
You can read more about the __doPostBack in this article:
Doing or Raising Postback using __doPostBack() function from Javascript in Asp.Net
Just doing a form.submit() will not be exactly the same as using __doPostBack.
To answer the first part of your question there is no difference doing a redirect if you are just doing a Response.Redirect as the will both do a GET. The difference is if you use a asp:Button control for instance, it will access your page first to handle the button (a post back) and then do a GET on the redirected page.
If you want to submit to the same URL (eg post your data) then you should use the __doPostBack method. If you don't require the data to be posted, then just do a redirect in javascript to the same URL with a modified query string (which will just do a basic GET) but your data will not be posted.
The only potential difference is that a querystring parameter is sent via GET, a form is (usually) sent by POST.
GET has a much smaller data limit as browsers have a max URL length (it varies)
You could use javascript to do a form.submit() which shoul emulate what ASP.Net does
I somewhat disagree with Basiclife's answer; if you have any code inside something like
if (IsPostBack) {
it's not going to be equivalent, ie the code is going to be executed if you're just setting the URL. Also, controls keep their state across postbacks but are freshly initialized if you're calling the URL again. This is due to ASP.NET trying to emulate a "normal" application, so the way to make sure a normal call and a postback have the same effect might result in "de-ASP.NET-ing" the entire page.
I'm not sure if what you want works. There probably is a way. But I heavily suspect there's a better way of doing this. If you get a postback for free, and can transmit data, why is it crucial that the data shows up in the URL, instead of being comfortably posted? I can see how you want a page to respond to a URL parameter, and how you might want to change the same parameter later on based on what's happening on that page, but since you always know you're posting back, you can eg override that URL parameter in that case, by something you're posting back. This doesn't sound so nice, but it might actually be less messy. Particularly since you seem to have a reason to not abandon the postback at all (otherwise you could just use a link, right?).

Categories

Resources