locked
have a small problem with XMLHttpRequest.open RRS feed

  • Question

  • User1366894300 posted

    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

    Thursday, January 16, 2014 12:55 PM

Answers

  • User-760709272 posted

    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.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 17, 2014 5:10 AM

All replies

  • User-933407369 posted

    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:

     xmlRequest.open("GET", "<%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx")%>?caseID=" + caseID + "&latlng=" + latlng, true);

    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.

    Friday, January 17, 2014 2:49 AM
  • User1734617369 posted

    Hi,

    If you are using an external javascript file you can not use server side code references, like in your case:

    <%=Page.ResolveUrl("~/GoogleMapCallbackHandler.ashx?")%>

    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

    Friday, January 17, 2014 3:49 AM
  • User1366894300 posted

    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

    Friday, January 17, 2014 5:02 AM
  • User1366894300 posted

    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

    Friday, January 17, 2014 5:09 AM
  • User-760709272 posted

    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.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 17, 2014 5:10 AM
  • User1366894300 posted

    yes this worked perfect many thanks

    //Tony

    Friday, January 17, 2014 12:31 PM