-2

I need 2 info in order to make an AJAX call.

  1. assessmentId
  2. classroomId

    $.ajax({

            url: "/rest/report/assignment",
            type: "POST",
            dataType: "json",
            data: {
                assessmentId: "206a9246-ce83-412b-b8ad-6b3e28be44e3",
                classroomId: "722bfadb-9774-4d59-9a47-89ac9a7a8f9a"
            },
    

I want to grab them from the URL params.

http://localhost:8080/BIM//teacher/reports/section-exercise/assignment?assessmentId=4d8d208e-8f71-4255-b54a-4c87a002314d&assessmentType=HOMEWORK&classroomId=722bfadb-9774-4d59-9a47-89ac9a7a8f9a&courseContentId=fd51fe9b-62ed-48f9-a6d8-5635f24964d6

Will I need to use <input type="hidden" /> some where ?

Any directions on this will be much appreciated !

code-8
  • 54,650
  • 106
  • 352
  • 604

1 Answers1

1

If you just want to grab that parameters from the URL, you could simple parse the URL by using for example the following function:

function parseUrl(url) {
  var urlParamSplit = url.split("?");

  if(urlParamSplit.length !== 2) return "InvalidUrlNoParamsSet";

  var paramsList = urlParamSplit[1].split("&");
  if(paramsList.length < 1) return "InvalidUrlNoParamsFound";

  var paramsObj = {};
  paramsList.forEach(function(item){
    var keyValueArray = item.split("=");
    paramsObj[keyValueArray[0]] = keyValueArray[1];
  });

  return paramsObj;
}

This is only possible if the parameter names never change and there are always some parameters set.

Your ajax call could then look like the following example:

var params = parseUrl("http://localhost:8080/BIM//teacher/reports/section-exercise/assignment?assessmentId=4d8d208e-8f71-4255-b54a-4c87a002314d&assessmentType=HOMEWORK&classroomId=722bfadb-9774-4d59-9a47-89ac9a7a8f9a&courseContentId=fd51fe9b-62ed-48f9-a6d8-5635f24964d6");
// your ajax call could look like this:
$.ajax({
  url: "/BIM/rest/report/assignment",
  type: "POST",
  dataType: "json",
  data: {
      assessmentId: params.assessmentId,
      classroomId: params.classroomId
  }, 
  // ... further settings and callbacks ...
});

You can get the current URL of the website via location.href. So you can call var params = parseUrl(location.href); to parse the current URLs parameters.

And you don't need to add a hidden input field.