locked
jQuery ajax call works in development PC but not in server RRS feed

  • Question

  • User206383436 posted

    This is my first experience with jQuery AJAX and I am using it in a VS2017 MVC project that works fine in my development machine. However, there is a problem when I upload and run it in my Windows Server 2012 cloud server. The code below makes the AJAX call in "function ObtenerSeccArtGen()" and the function in the "error" parameter is triggered showing the 3 "alerts". First alert displays "Error". Second alert displays "0". Third alert does not display anything. I have reviewed the code for some time and even included "alert" commands to display information up to the variable "strURL" which is correctly displayed. 

    I will appreciate any clue about what to look for. 
    Thank you in advance.

    var intOffset = 0;
    var intPrimerIndice = 0;
    
    /* ************************************************************************************
        Load and show first set of records when document is ready.
    ************************************************************************************ */
    $(document).ready
    (
        ObtenerSeccArtGen()
    );
    
    /* ************************************************************************************
                Se detecta el fin de la pantalla y se cargan más registros.
    ************************************************************************************ */
    $(window).on("scroll", function () {
        var scrollHeight = $(document).height();
        var scrollPosition = $(window).height() + $(window).scrollTop();
        if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
            ObtenerSeccArtGen();
        }
    });
    
    
    /* ************************************************************************************
                                Se consume la Web API.
    ************************************************************************************ */
    function ObtenerSeccArtGen()
    {
        var value = $("#txtSeccion").val(); 
        //var strURL = encodeURI("http://localhost:60642/api/SeccArtGen/" + $("#txtSeccion").val() + "-" + intOffset);
        var strURL = encodeURI("http://www.lasuperlista.com/api/SeccArtGen/" + $("#txtSeccion").val() + "-" + intOffset);
        alert("strURL: " + strURL);
    $.ajax ({ type: "GET", dataType: "json", url: strURL, success: function (result) { DespliegaSeccArtGen(result); }, error: function (xhr, ajaxOptions, thrownError) { alert('Error'); alert(xhr.status); alert(thrownError); } }); } /* ************************************************************************************ Despliega los registros de la sección. ************************************************************************************ */ function DespliegaSeccArtGen(result) { var $mainContainerDiv = $("#mainContainer"); var $secondaryContainerDiv = $("#secondaryContainer"); // El primer registro se despliega con formato diferente a los demás. if (intOffset == 0) { var currentSeccArtGen = result[0]; var $rowDiv = $("<div class='row'></div>").appendTo($secondaryContainerDiv); var $contenidoDiv = $("<div class='col-lg-9 col-md-9 col-sm-9 col-xs-9'></div>").appendTo($rowDiv); var $publicidadDiv = $("<div class='col-lg-3 col-md-3 col-sm-3 col-xs-3'></div>").appendTo($rowDiv); var $imagen = $("<div><img class='img-responsive sag-imagen' src='" + currentSeccArtGen.foto_grande + "' onclick='ObtenerArtGen(" + currentSeccArtGen.sag_clave +")'/></div>").appendTo($contenidoDiv); var $fecha = $("<div class='sag-fecha-primer-registro'>" + currentSeccArtGen.sag_fipub_aux + "</div>").appendTo($contenidoDiv); var $enca = $("<div class='sag-enca-primer-registro'>" + currentSeccArtGen.sag_enca + "</div>").appendTo($contenidoDiv); var $subenca = $("<div class='sag-subenca-primer-registro'>" + currentSeccArtGen.sag_subenca + "</div>").appendTo($contenidoDiv); //var $publicidad = $("<div><img class='img-responsive' style='padding-top:15px;' src='http://localhost:60642/Content/SiteImages/Ad_250x200.png' /></div>").appendTo($publicidadDiv); var $publicidad = $("<div><img class='img-responsive' style='padding-top:15px;' src='http://www.lasuperlista.com/Content/SiteImages/Ad_250x200.png' /></div>").appendTo($publicidadDiv); intPrimerIndice = 1; } // Se despliegan los registros del segundo en adelante. for (var i = intPrimerIndice; i < result.length; i++) { var currentSeccArtGen = result[i]; var $rowDiv = $("<div class='row'></div>").appendTo($secondaryContainerDiv); var $imagenDiv = $("<div class='col-lg-5 col-md-5 col-sm-12 col-xs-12'></div>").appendTo($rowDiv); var $textoDiv = $("<div class='col-lg-7 col-md-7 col-sm-12 col-xs-12'></div>").appendTo($rowDiv); var $imagen = $("<div><img class='img-responsive sag-imagen' src='" + currentSeccArtGen.foto_grande + "' onclick='ObtenerArtGen(" + currentSeccArtGen.sag_clave + ")'/></div>").appendTo($imagenDiv); var $fecha = $("<div class='sag-fecha'>" + currentSeccArtGen.sag_fipub_aux + "</div>").appendTo($textoDiv); var $enca = $("<div class='sag-enca'>" + currentSeccArtGen.sag_enca + "</div>").appendTo($textoDiv); var $subenca = $("<div class='sag-subenca'>" + currentSeccArtGen.sag_subenca + "</div>").appendTo($textoDiv); } // A partir de intOffset > 0 se despliegan todos los registros. // Solo si intOffset == 0 se despliega el primer registro de manera independiente. intPrimerIndice = 0; intOffset++; }

    Monday, April 30, 2018 12:15 AM

Answers

  • User186824947 posted

    Hi Jorgemal,

    Yes, I can see it works if the link contains "www". I also open http://lasuperlista.com/SeccArtGen/Index/Notas on my side. And I think I find the root cause by using IE developer tool (F12). Here is the error message: 

    So this is CORS issue. Please also enable cors for this origin (http://lasuperlista.com) in API side. Hope this could fix your issue.

    Best Regards,

    Jambor

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 1, 2018 1:26 AM

All replies

  • User186824947 posted

    Hi Jorgemal,

    >First alert displays "Error". Second alert displays "0"

    There are a lot of issues caused  statue code 0 error. There is a topic discuss a lot about Status Code 0. Please have a look at it:  https://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa 

    I have also tested endpoint: http://www.lasuperlista.com/api/SeccArtGen/{some value}-{offset}. As I test, this endpoint is not encrypted. And it give me response (I don't know the value of this part {some value}-{offset}):

    The format of the data seems XML not json. Please also check your API. If you still could not find the root cause and the data is not sensitive, please feel free follow up and give me the value of this part {some value}-{offset}. 

    Best Regards,

    Jambor

    Monday, April 30, 2018 3:10 AM
  • User206383436 posted

    Thanks for your reply.

    You can try this: http://lasuperlista.com/SeccArtGen/Index/Notas
    The offset is calculated and not needed to get to the page. It serves the purpose of displaying data in chunks of 20 records (more or less). This means that it is detected when the user reaches the end of the page and more records are loaded and displayed automatically.

    This testing website is located at www.lasuperlista.com and you can click any image in the banner rotator to experience this issue.

    I am going over the link you provided about status code 0.

    Best regards,
    Jorge Maldonado

    Monday, April 30, 2018 4:58 PM
  • User206383436 posted

    I just noticed that the page works if I use www.lasuperlista.com

    The issue appears if using only lasuperlista.com without www

    The URL parameter in the AJAX call includes http://www.lasuperlista.com so maybe a XSS attack might be detected when using only lasuperlista.com

    I will appreciate your comments.

    Regards,
    Jorge Maldonado

    Monday, April 30, 2018 5:11 PM
  • User186824947 posted

    Hi Jorgemal,

    Yes, I can see it works if the link contains "www". I also open http://lasuperlista.com/SeccArtGen/Index/Notas on my side. And I think I find the root cause by using IE developer tool (F12). Here is the error message: 

    So this is CORS issue. Please also enable cors for this origin (http://lasuperlista.com) in API side. Hope this could fix your issue.

    Best Regards,

    Jambor

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 1, 2018 1:26 AM
  • User206383436 posted

    Thanks for your valuable help.

    This is a link to a video that shows how to implement CORS in ASP.NET.
    https://www.youtube.com/watch?v=nsnuIZX1C9A

    The video uses Visual Studio 2015 NuGet package installer to get a package you will need but I did not find such a package in VS2017 so I used the following link to install it from a PM command prompt:
    https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors/5.2.5

    Thanks.

    Saturday, May 5, 2018 5:43 PM