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

5
  • Type varies on the method that you use on your form. If post method then type should be post also.. Commented Mar 6, 2014 at 9:36
  • 1
    did you try this ? url:"myURL?accountCode="+stringAccountCode Commented Mar 6, 2014 at 9:36
  • @SandeepNayak You have a typo, should be var myURL = "WebApp/Data?accountCode=" + stringAccountCode ; or url:myURL+ "?accountCode="+stringAccountCode Commented Mar 6, 2014 at 9:38
  • @Pavlo: Agree. You are right. I didnt notice the myURL variable. Commented Mar 6, 2014 at 9:39
  • I've tried this but i get 400 error - bad request. Is this not possible to achieve using jquery's '$.param' - or url request parameter Commented Mar 6, 2014 at 10:07

1 Answer 1

3

Try,

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

Appending number of parameters you want example

?accountCode="+stringAccountCode+"&aa="+someAccount
Sign up to request clarification or add additional context in comments.

4 Comments

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
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", })
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.