skip to Main Content

I am trying to pass a list of subject id’s as a query param in an Ajax get-request, so that it only returns data pertaining to the subject id’s I passed in. The API is set up to receive a subjectId param as a single number, or string of numbers separated by commas. I have made sure that what I am sending is exactly that — "13,14,15" — but when I make the request, the get URL encodes the string so that it looks like this: 13%2C14%2C15%2C.

The URL I want to generate is (I’m just using placeholders for the domain name and session token) https://get-url.com/get-filter-counts?sessionToken=abcdefg&subjectId=13,14,15. When I test this out in the browser, it works.

The URL I’m actually generating is https://get-url.com/get-filter-counts?sessionToken=abcdefg&subjectId=13%2C14%2C15%2C

I’ve tried researching this issue but I’m not even sure what it is that’s happening there. I’ve looked at a ton of examples of passing strings as query params in an Ajax request, and as far as I can tell I’m doing it correctly. I’ve hard-coded a string into the params below just to demonstrate:

 $.ajax({
      type: "GET",
      url: getURL,
      dataType: "JSON",
      data: {
        sessionToken: sessionToken,
        subjectId: "13,14,15"
      },

      //process the returned result of the Ajax call
      success: (ajaxResult) => {
        console.log("subject id list:", subjectId);
        console.log("Ajax result:", ajaxResult);
      },

In the success method, the console returns the correct subjectId list as well as the data pertaining to those subject id’s. But I am unable to get the results in the browser because of this URL issue.

How can I remove the encoding (if that’s what’s happening?) from the string in the url? Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    Well, I was sending the string of numbers with a trailing comma at the end (I didn't think that would be an issue), and it turns out that's why the server wasn't properly handling the encoded URL. Just sharing in case this is useful for anyone else who looks at this question.

    The comment from @Quentin was essentially correct, except the issue was in fact a frontend typo.


  2. The issue is with using commas in-between numbers. The string you are sending from the frontend is getting encoded and hence the URL you see. If you try encodeURIComponent(13,14,15) you’ll see the same response.
    The solution would be using something other than commas and handling that on the backend or simply sending an array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search