简体   繁体   中英

Passing a variable to URL parameters using JQuery POST

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

Try,

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

Appending number of parameters you want example

?accountCode="+stringAccountCode+"&aa="+someAccount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM