3

I have a function which currently passes an account code (derived from a combo box) to the server. Currently it does this by sending the request in the body - I need it to send as a URL parameter. So for example the URL should be:

localhost:1234/myProject/WebApp/Data?accountCode=Full

Assuming full is selected.

My code below works as a request body but my attempts to amend it to submit as a URL request have failed.

 accountSelected: function () {
                var saccountCode = $("select#accountcombo").val();
                var stringAccountCode = saccountCode.toString()
                console.log("Account is: " + stringAccountCode);
                var myURL = "WebApp/Data";
                $.ajax({
                    url: myURL,
                    type: "POST",
                    data: {
                        "accountCode": stringAccountCode
                    },
                    dataType: "text",
                })

I have been looking at using $.param but couldn't get anything to work and also read on other questions about using $.get but when I change my code above to a "GET" i get an error "Request method 'GET' not supported" - the server is expecting a POST request. Any way i could achieve this?

Thanks

Suraj Shrestha
  • 1,790
  • 1
  • 25
  • 51
Hagbard
  • 575
  • 5
  • 8
  • 18

1 Answers1

3

Try,

URL: "localhost:1234/myProject/WebApp/Data?accountCode="+stringAccountCode

Appending number of parameters you want example

?accountCode="+stringAccountCode+"&aa="+someAccount
Santhosh Kumar
  • 190
  • 3
  • 10
  • Is there a way of displaying the result in the URL bar - at the moment i can view it in console and i get 'Error: myProject/WebApp?accountCode=Full' I believe the error is unrelated but i wanted to display the result of the POST in the URL bar which doesnt seem to happen – Hagbard Mar 06 '14 at 10:42
  • You are trying to do the ajax which happen instead of POST pls try the GET that's the error. What you trying is GET method `$.ajax({ url: myProject/WebApp?accountCode=Full, type: "GET", dataType: "text", })` – Santhosh Kumar Mar 06 '14 at 11:14
  • Okay thanks, the key bits work - i still need to use a POST - but can get away without sticking the URL in the bar - so long as the correct data is returned. Cheers Also thanks to Sandeep and Pavlo who went down the same route. – Hagbard Mar 06 '14 at 11:25
  • Just a note in case someone else comes across this, shouldn't encodeURIComponent be used here as well. Granted an account number is unlikely to have characters needing escaped. – David Bradley Aug 24 '18 at 16:19