Forum Discussion

Kev_B's avatar
Kev_B
Advisor
2 months ago

400/404 error on POST request

Hey, I'm trying to do my very first POST API call and currently getting stuck. I know there are other solutions for what I'm trying to do, but I'm also trying to learn something rather than straight...
  • Claudius's avatar
    2 months ago

    It also took me a bit of back and forth to get it working. Reasons are due to Khoros Classic using an older version of jQuery and some specific JSON requirements:

    1. Since Classic's jQuery version is older you need to use type: "POST" instead of method: "POST"
    2. The contentType "application/json" needs to be specified
    3. data parameter needs to be stringified (you already were on the right tracke with your v2)

    Here's the resulting evolution of your v1 code that works on my end:

    <#assign boardID = coreNode.id />
    
    <button id="subscribe">AJAX test</button>
    
    <@liaAddScript>
      (function($){
        $(document).ready(function(){
          $("#subscribe").on( "click", function() {
            $.ajax({
    	  url: "/api/2.0/subscriptions",
    	  contentType: "application/json",
    	  dataType: 'json',
    	  data: JSON.stringify({
    	      type:"subscription",
    	      target:{
    	        type:"board",
                    id:"blueprints"
    	    }
    	  }),
    	  type: 'POST'
    	}).done(function(response) {
    	  console.log('Success');
    	}).fail(function() {
    	  console.log('Fail');
    	})
          });
        });
      })(LITHIUM.jQuery);
    </@liaAddScript>

    Bonus tip:
    Looking at the details in your browser's network tab will reveal that quite often the HTTP error code is hiding a different more detailed "developer message" debug info in the return data. E.g. here's a HTTP 500 error that is hiding a message about non-stringified content: