Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Last active June 14, 2024 06:27
Show Gist options
  • Save tranchausky/5d115183bc50e3f30c3bc9766518ccd7 to your computer and use it in GitHub Desktop.
Save tranchausky/5d115183bc50e3f30c3bc9766518ccd7 to your computer and use it in GitHub Desktop.
jquery send ajax json
var settings = {
"url": "https://website.local:8443/api/event-checkin",
"method": "POST",
"timeout": 0,
"headers": {
"username": "usename",
"password": "password",
"Content-Type": "application/json"
},
"data": JSON.stringify({
"events": [
{
"name": "1",
"email": "[email protected]",
"birthday_month": "11",
"birthday_day": "11",
"birthday_year": "2024",
"phone": "",
"year": "",
"make": "",
"model": "",
"options": ""
}
]
}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="form" action="" method="post">
<textarea name="events" id="data">{ "events" : [ { "name" : "1", "email" : "[email protected]", "birthday_month": "11", "birthday_day": "11", "birthday_year": "2024", "phone": "", "year": "", "make": "", "model": "", "options": "" } ] }</textarea>
<input id="submit" type="button" name="submit" value="submit">
</form>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
// click on button submit
$("#submit").on('click', function(){
var datasend = $('#data').val();
$.ajax({
headers: {
'username': 'usename',
'password': 'password',
"Content-Type": "application/json"
},
url: 'https://website.local:8443/api/event-checkin', // url where to submit the request
type : "POST", // type of action POST || GET
//data : JSON.stringify(datasend), // post data || get data
data : datasend, // post data || get data
success : function(result) {
console.log(result);
},
error: function(xhr, resp, text) {
console.log(xhr, resp, text);
}
})
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment