A spelling mistake you should get out of the way: "cosole".
| orig%5Bcity%5D=Albuquerque&orig%5Bstate%5D=NM&orig%5Bcountry%5D=US&dests%5B0%5D%5Bcity%5D=Los+Angeles&dests%5B0%5D%5Bstate%5D=CA&dests%5B0%5D%5Bcountry%5D=US&dests%5B1%5D%5Bcity%5D=San+Diego&dests%5B1%5D%5Bstate%5D=CA&dests%5B1%5D%5Bcountry%5D=US&dests%5B2%5D%5Bcity%5D= New+York&dests%5B2%5D%5Bstate%5D=NY&dests%5B2%5D%5Bcountry%5D=US |
Enclose this string in quotes and set the value to the variable, JSONStr. You will see why the request failed when you do
<cfoutput>#URLDecode(JSONStr)#</cfoutput>
The result contains characters &. It suggests that this string consists of a series of key-value pairs in a URL query-string.
It's seems a super big flaw that this isn't supported in coldfusion |
What you want is actually supported in ColdFusion. A possible solution is to add the content-type to your request. An example follows.
JSON_post_using_JQuery.cfm
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var JSONObject = {
orig: {
city: "Albuquerque", state: "NM", country: "US"
},
dests: [
{city: "Los Angeles", state: "CA", country: "US"},
{city: "San Diego", state: "CA", country: "US"},
{city: "New York", state: "NY", country: "US"}
]
};
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: "POST",
url: "http://127.0.0.1:8500/CFProject/JSONPost.cfc?method=examplePostEndpoint",
contentType: "application/json",
data: JSON.stringify(JSONObject),
success: function(data) {
console.log("Success:", data);
},
error: function(data) {
console.log("Failure:", data);
}
});console.log("Executed");
});
});
</script>
<button>Send HTTP POST request to CFC</button>
\wwwroot\CFProject\JSONPost.cfc
<cfcomponent >
<cffunction name="examplePostEndpoint"
consumes="application/json"
produces="application/json"
httpMethod="POST"
returntype="string"
returnformat="json"
access="remote">
<cflog text="#serializeJSON(getHttpRequestData().content)#" file="JSONPostTest">
</cffunction>
</cfcomponent>