locked
jQuery - pass parameter in url RRS feed

  • Question

  • I am trying to send a request to my server using jQuery/ajax: in this call, i need to transmit a string, a to a file. However, all I get is something this:

    D%3AMusicFLAC%20ArchivierungAmy%20McDonaldThis%20is%20the%20Life%07%20Barrowland%20Ballroom.flac

    Clearly, the backslashes are misinterpreted by JavaScript, so i tried using escape(), encodeURI() etc combined, each alone and various combinations, but still could not make it work.

    This is my code:

    <button onclick="Start('File', escape('@item.Pfad\@item.Name'))">Click!</button>
    
    function Start(PlayerType, Pfad) {
    $.ajax({
        url: "Player/Start",
        type: "get",
        data: {
            Typ: PlayerType,
            Pfad: encodeURIComponent(Pfad)
        },
        dataType: "html",
        success: function (result) {
            $("#PlayerDIV").html(result);
        }
    });
    }
    
    public ActionResult Start(string Typ, string Path)
        {
            Debug.WriteLine(HttpUtility.UrlDecode(Pfad));
            ...
        }


    Thank you for your help!

    Thursday, February 23, 2017 6:16 AM

Answers

  • Hey, thank you for your quick reply!

    Why exactly would that make a difference to the string that is being passed? After all, a "normal" string ie one without backslashes is working just fine...


    Because of encapsulation, the data is not affected when passing backslash or other special characters that can become corrupted when passed on the URL as a parm. By passing the data in the object, the data is hidden.

    https://www.tutorialspoint.com/java/java_encapsulation.htm

    Java or .NET the explanation is the same.

    Friday, February 24, 2017 2:17 AM

All replies

  • Have the get method accept an object like a DTO. Your data in the ajax call should map to properties of the DTO. Your passed parms are encapsulated inside the DTO.

    Get(PassedDTO)

    {

      string something =PassedDTO.Typ.

    }

    Public class PassedDTO

    {

      public string Typ {get; set; }
      public string Pfad {get; set;}

    }

    https://en.wikipedia.org/wiki/Data_transfer_object

    Thursday, February 23, 2017 2:54 PM
  • Hey, thank you for your quick reply!

    Why exactly would that make a difference to the string that is being passed? After all, a "normal" string ie one without backslashes is working just fine...


    Thursday, February 23, 2017 3:25 PM
  • Hey, thank you for your quick reply!

    Why exactly would that make a difference to the string that is being passed? After all, a "normal" string ie one without backslashes is working just fine...


    Because of encapsulation, the data is not affected when passing backslash or other special characters that can become corrupted when passed on the URL as a parm. By passing the data in the object, the data is hidden.

    https://www.tutorialspoint.com/java/java_encapsulation.htm

    Java or .NET the explanation is the same.

    Friday, February 24, 2017 2:17 AM
  • Perfect, thank you for your help!

    Friday, February 24, 2017 6:00 AM