简体   繁体   中英

Passing JavaScript Data onClick()

Hope Everyone is doing good.

I want to pass data on button click. This is my code, I am not getting it on other side.

 <script> function submit() { var url = "http://localhost:8080" + "/submit" var data = {"order":"Full"}; $.post(url, {}, function(data, status, xhr) { alert('status: ' + status + ', data: ' + JSON.stringify(data)); }, 'json' ); } </script>
 <button onClick="submit()">submit</button>

You are passing an empty object as the data of the POST request. Change the $.post call to this.

$.post(url, data,
  function(data, status, xhr) {
    alert('status: ' + status + ', data: ' + JSON.stringify(data));
  },
  'json'
);

You can also try below with ajax function

var oParameters = {
                attributeValue : 'myAttributeValue',
                attributeType: 'myAttributeType'
           };

        $.ajax({
            url: "http://localhost:8080" + "/submit",
            type: "POST",
            data: oParameters,
            success: function(data) {
                alert("Data submitted successfully....");
            },
            error: function(data) {
                alert("Error while submitting Data");
            }
        });

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