When I have this function CallServerForUpdate in the external js library there is problem with xmlRequest.open because
when I run it says CallServerForUpdate not defined.
When I have this function CallServerForUpdate local in the content page averything works as it should
I have the master page in this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime
I have the content page in this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime\coordinator
I have the external js file in this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime\js
I have the ashx file GoogleMapCallbackHandler.ashx in the same folder as the master page
The site root folder is this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime
I must say this isn't a huge problem because I just put this small java script function local as I said before.
But it would also be great to know why I can't have the CallServerForUpdate in the external java script library
Any suggestion will be helpful ?
function CallServerForUpdate()
{
var latlng = latitud + "$" + longitud;
var caseID = document.getElementById("ContentPlaceHolder2_arendeDetaljUsrCtrl_lblID").innerHTML;
xmlRequest.open("GET", "<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx?")%>caseID=" + caseID + "&latlng=" + latlng, true);
Had you debug your code? what's the result of the data( e.g:‘"<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx")%>?caseID=" + caseID + "&latlng=" + latlng’) .
that will not work and will probably cause the kind of errors that you see. If you need to pass that kind of parameter to the function you have to do that from the page where the function is called.
The reason I have this xmlRequest.open("GET","<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx")%>?caseID="+ caseID +"&latlng="+ latlng,true);
is because this is not static data. As you can see I send the value of caseId and the value of
latlng.
If I would send static data like this example from w3school
xmlhttp.open("GET","demo_get2.asp?fname=Henry&lname=Ford",true);
I could use the same principle but in my case I think I must use this page.ResolveUrl because of passing dynamic data.
I can't debug this because as soon as this function is to be called I get CallServerForUpdate is undefined
So why is it not possible to have this statement
xmlRequest.open("GET", "<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx?")%>caseID=" + caseID + "&latlng=" + latlng, true);
in external javascript when it works perfect when I have this local in the aspx page content page.
You say "If you are using an external javascript file you can not use server side code references"
with Page.ResolveUrl and so on
It seems that you know the reason ?
When you move your javascript into an include js file it becomes a flat file, no different from a txt file or a jpg, so when it is requested it doesn't go through the .net pipeline, so you can't have any code in it. Your .net is being interpreted as literal
javascript which isn't valid js so the whole function is considered not defined so you can't call it. There is an awkward workaround for this, where you define the dynamic url on your page, then use the url in your script file.
On your aspx page have this
<script type="text\javascript">
var handlerUrl = '<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx")%>';
</script>
Change your js to be like this;
function CallServerForUpdate()
{
var latlng = latitud + "$" + longitud;
var caseID = document.getElementById("ContentPlaceHolder2_arendeDetaljUsrCtrl_lblID").innerHTML;
xmlRequest.open("GET", handlerUrl + "?caseID=" + caseID + "&latlng=" + latlng, true);
xmlRequest.onreadystatechange = ApplyUpdate;
xmlRequest.send(null);
}
It doesn't matter than the variable is defined somewhere different from where it is used, the above will still work.
I'm afraid I no longer use this forum due to the new point allocation system.
Member
75 Points
540 Posts
have a small problem with XMLHttpRequest.open
Jan 16, 2014 12:55 PM|Tojo|LINK
I use master page/content page
When I have this function CallServerForUpdate in the external js library there is problem with xmlRequest.open because
when I run it says CallServerForUpdate not defined.
When I have this function CallServerForUpdate local in the content page averything works as it should
I have the master page in this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime
I have the content page in this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime\coordinator
I have the external js file in this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime\js
I have the ashx file GoogleMapCallbackHandler.ashx in the same folder as the master page
The site root folder is this location C:\Users\tony\programmering\aspNet\Gotland\Environment_Crime
I must say this isn't a huge problem because I just put this small java script function local as I said before.
But it would also be great to know why I can't have the CallServerForUpdate in the external java script library
Any suggestion will be helpful ?
function CallServerForUpdate()
{
var latlng = latitud + "$" + longitud;
var caseID = document.getElementById("ContentPlaceHolder2_arendeDetaljUsrCtrl_lblID").innerHTML;
xmlRequest.open("GET", "<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx?")%>caseID=" + caseID + "&latlng=" + latlng, true);
xmlRequest.onreadystatechange = ApplyUpdate;
xmlRequest.send(null);
}
//Tony
All-Star
15648 Points
2151 Posts
Re: have a small problem with XMLHttpRequest.open
Jan 17, 2014 02:49 AM|Happy Chen - MSFT|LINK
hi Tojo,
Had you debug your code? what's the result of the data( e.g:‘"<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx")%>?caseID=" + caseID + "&latlng=" + latlng’) .
i would suggest you try to use the code below:
The XMLHttpRequest object is used to exchange data with a server.
If you want to send information with the GET method, add the information to the URL:
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_get2
Hope it helps you.
Participant
1390 Points
323 Posts
Re: have a small problem with XMLHttpRequest.open
Jan 17, 2014 03:49 AM|valuja|LINK
Hi,
If you are using an external javascript file you can not use server side code references, like in your case:
that will not work and will probably cause the kind of errors that you see. If you need to pass that kind of parameter to the function you have to do that from the page where the function is called.
Best regards
Johan
Member
75 Points
540 Posts
Re: have a small problem with XMLHttpRequest.open
Jan 17, 2014 05:02 AM|Tojo|LINK
The reason I have this xmlRequest.open("GET", "<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx")%>?caseID=" + caseID + "&latlng=" + latlng, true);
is because this is not static data. As you can see I send the value of caseId and the value of latlng.
If I would send static data like this example from w3school
xmlhttp.open("GET","demo_get2.asp?fname=Henry&lname=Ford",true);
I could use the same principle but in my case I think I must use this page.ResolveUrl because of passing dynamic data.
I can't debug this because as soon as this function is to be called I get CallServerForUpdate is undefined
//Tony
Member
75 Points
540 Posts
Re: have a small problem with XMLHttpRequest.open
Jan 17, 2014 05:09 AM|Tojo|LINK
So why is it not possible to have this statement
xmlRequest.open("GET", "<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx?")%>caseID=" + caseID + "&latlng=" + latlng, true);
in external javascript when it works perfect when I have this local in the aspx page content page.
You say "If you are using an external javascript file you can not use server side code references"
with Page.ResolveUrl and so on
It seems that you know the reason ?
//Tony
All-Star
37441 Points
9076 Posts
Re: have a small problem with XMLHttpRequest.open
Jan 17, 2014 05:10 AM|AidyF|LINK
When you move your javascript into an include js file it becomes a flat file, no different from a txt file or a jpg, so when it is requested it doesn't go through the .net pipeline, so you can't have any code in it. Your .net is being interpreted as literal javascript which isn't valid js so the whole function is considered not defined so you can't call it. There is an awkward workaround for this, where you define the dynamic url on your page, then use the url in your script file.
On your aspx page have this
Change your js to be like this;
It doesn't matter than the variable is defined somewhere different from where it is used, the above will still work.
Member
75 Points
540 Posts
Re: have a small problem with XMLHttpRequest.open
Jan 17, 2014 12:31 PM|Tojo|LINK
yes this worked perfect many thanks
//Tony