Added a method to send JSON-encoded data.

This commit is contained in:
Josh Hall-Bachner 2013-06-03 19:11:42 -07:00
parent ce1b40eac3
commit 705e01625e

View File

@ -16,6 +16,11 @@
<div id="header">
<a href=""><h1>{{ apiName }}</h1></a>
<div id="sandbox_configuration">
content format:
<select id="content_format">
<option value="x-www-form-urlencoded">Form Data</option>
<option value="json">JSON</option>
</select>
request format:
<select id="request_format">
<option value="json"{{ defaultRequestFormat == 'json' ? ' selected' : '' }}>JSON</option>
@ -161,6 +166,9 @@
headers['Accept'] = 'application/' + requestFormat;
}
// set content format
var contentFormat = $('#content_format').val();
// retrieve all the parameters to send
$('.parameters .tuple', $(this)).each(function() {
var key, value;
@ -183,6 +191,10 @@
if (value) {
headers[key] = value;
}
if(!('Content-type' in headers)) {
headers['Content-type'] = 'application/'+contentFormat;
}
});
// fix parameters in URL
@ -212,11 +224,20 @@
endpoint = $('#api_endpoint').val();
}
// prepare final parameters
var body = params;
if(contentFormat == 'json') {
body = JSON.stringify(body);
}
var data = content.length ? content : body;
console.log(contentFormat); console.log(data);
// and trigger the API call
$.ajax({
url: endpoint + url,
type: method,
data: content.length ? content : params,
data: data,
headers: headers,
crossDomain: true,
beforeSend: function (xhr) {