How to pass a javascript variable in an ajax url? - javascript

I have a variable as per below:
var myip;
And I want to insert it in the url below:
$.ajax('http://api.ipstack.com/**[myip]**?access_key=mykey')
If I manually put in my ip in place of [myip] it gives me the desired results but I want to automate it.
I tried with the methods given in the url below but it didn't help.
How to pass Javascript variables inside a URL? AJAX
Thanks in advance for going through and helping!

Use string template.
$.ajax(`http://api.ipstack.com/${myip}?access_key=mykey`)
^ ^^^^^^^ ^
Or using string concatenation.
$.ajax('http://api.ipstack.com/' + myip + '?access_key=mykey')

Related

Open Oracle Apex URL with JavaScript when giving parameters

when i use javascript eval() to open apex url from js i have no problem when i use eval() like this
eval("f?p=&APP_ID.:7:&SESSION.");
but when i wanna pass parameters with eval() like this
eval("f?p=&APP_ID.:7:&`SESSION.:P7_ID:8461,P7_ALLOWCHANGE:1,P7_WFDEF_ID:69004.");`
i get this error: SyntaxError: expected expression, got ':'
then these parameters automatically added after generating url
javascript:apex.navigation.dialog('f?p=101:7:28809985622510:::::\u0026p_dialog_cs=_7P7TVFV5LTQPjeyg-bGqSKpcYM',{title:'Workflow State',height:'auto',width:'720',maxWidth:'960',modal:true,dialog:null},'t-Dialog-page--standard '+'',this);:P7_ID:8461,P7_ALLOWCHANGE:1,P7_WFDEF_ID:69004.;
what should i do?
The best way would to be to use apex_page.get_url
It is so much simpler to use than apex_util.prepare_url
https://docs.oracle.com/cd/E59726_01/doc.50/e39149/apex_page.htm#AEAPI30190
I don't know about your eval call, but this APEX URL syntax is wrong:
f?p=&APP_ID.:7:&`SESSION.:P7_ID:8461,P7_ALLOWCHANGE:1,P7_WFDEF_ID:69004.
All the item names should be listed together, then all the values together - and after the correct number of colon separators:
f?p=&APP_ID.:7:&SESSION.::::P7_ID,P7_ALLOWCHANGE,P7_WFDEF_ID:8461,1,69004
I also removed the spurious back-tick character from before "SESSION".
Apart from what Tony Andrews covered, here are a few more issues with your URL:
1. it's APP_SESSION - not SESSION. Here's documentation on built in substitution strings.
2. Your items are not substituted properly. Read this documentation page more details on substitutions in APEX.
Here's documentation on understanding APEX URL syntax.
Secondly, here's what you would try. Create a hidden page item and use APEX_UTIL.PREPARE_URL function and generate valid url, assign to the item. And use that item as url in your javascript. I haven't tried this, but this would be a better approach, I think.
Also prepare url like this:
APEX_UTIL.PREPARE_URL('f?p=' || :APP_ID ||':7:' || :APP_SESSION || :::' ||:P7_ID: ',' || :P7_ALLOWCHANGE || ',' || :P7_WFDEF_ID || ':8461,1,69004')
Here's another great resource to understand apex url and how to pass variables:
http://dgielis.blogspot.in/2015/01/understanding-apex-url-passing.html

Variable in AJAX url

How can I use a variable within an AJAX call URL?
So far I have the following:
id = $(this).children('span.title').attr('data-id');
$("#test").load("http://localhost:8888/projects/superfreerespo/ + id/ .gameBox-Ops");
There is something wrong with the way I have declared the url but being relatively knew to Ajax I am a little unsure where I am going wrong.
Just to point out the .gameBox-Ops is not part of the URL, it is the class of the container I am trying to call with AJAX
id is a variable, you have to escape the string to concatenate it into it.
$("#test").load("http://localhost:8888/projects/superfreerespo/"+id"+/ .gameBox-Ops");
$("#test").load("http://localhost:8888/projects/superfreerespo/" + id + "/ .gameBox-Ops");
Not related to AJAX, your string concatenation is wrong.
Use proper string concatenation. Also I am not sure why you have . on last part.
id = $(this).children('span.title').attr('data-id');
$("#test").load("http://localhost:8888/projects/superfreerespo/" + id + "/.gameBox-Ops");

How does one safely pass a url and filename as part of a query string?

Say I have a url: http://foo.com
Normally if I wanted to pass any parameters to it I would do something like:
http://foo.com?param1=car&param2=red
But what if I wanted to pass a url and filename instead ?
I can't just do this:
http://foo.com?url=http://bar.com/kitty.jpg&filename=kitty.jpg
Because there are a number of characters that I might have to escape. I create the url in javascript, so I can use the Javascript API to construct the url. Any suggestion on how to do this, and which pitfalls to watch out for ? How to safely create the url?
All you need is encodeURIComponent - simple example:
var url='http://bar.com/kitty.jpg';
var filename='kitty.jpg';
var completeUrl='http://foo.com'+
'?url='+encodeURIComponent(url)+
'&filename='+encodeURIComponent(filename);

How to pass javascript url parameter in this script through html dom?

I want to pass url parameter through html dom. This is the javascript i am using
<script>
function myFunction(url)
{
setTimeout(function(){window.location.assign(url)},4000);
}
</script>
HTML
Click here
But its not working. How can i fix this situation ? Thanks for your answers.
Use combination of single and double quotes, to pass the url as string literal. If the url is not enclosed in quotes it is considered as some variable and javascript could nod find that.
Click here
As #Adil says, you need to pass the argument as string to the function. In the way you are trying, the script is trying to recover the value of the http://stackoverflow.com variable.
what #Adil has said is true:
Click here
but if you want to pass the url as a parameter do not forget to change your function like this:
function myFunction(url)
{
setTimeout(function(){window.location.assign(encodeURIComponent(url)},4000);
}

Getting part of referring url with jquery

I'm trying to set a cookie with a value that is made up of part of a referring url. The referring url looks something like:
http://webct.university.ac.uk/webct/urw/lc715920578061.tp721521425061/courseMenu.dowebct?
and I need to extract:
lc715920578061.tp721521425061
for reuse in another function.
Would this be regex? Could anyone help with the syntax?
Many thanks
Matthew
You can use replace with a regex and split like this:
var desired_part = document.referrer.replace(/^https?:\/\//gi, '').split('/')[3];

Categories

Resources