0

I made this POST request to the MOXTRA API to create a new binder:

<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
<script type="text/javascript" src="https://www.moxtra.com/api/js/moxtra-latest.js" id="moxtrajs" data-client-id="nJTHiclOwZA" data-app-key="urLRETVepwA"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button id="btn1">Show Text</button>

<script>
  $("button").click(function(){
    $.post({
      url: "https://api.moxtra.com/me/binders?access_token=vwowMQAAAUV91nUdAACowFVFbXhvZ1ptWlZjRWdiUGVhTTNtN2JIAAAAA1R1YVAzVGhGSTNSOEdsakRkWU53VjE2bkpUSGljbE93WkE",
      data: {"name": "My First Binder"},
      success: function(ans){
        $( "p" ).html( ans);
      }
    });
  });
</script>

</body>
</html>

The access key and client id are all correct but still the code is not working . Can anyone highlight what is wrong

Rupam Datta
  • 1,849
  • 1
  • 21
  • 36
vineeth
  • 16
  • 5
  • you can't post to that domain, unless your on that domain. Post to a page on your server that posts to that domain, output response. – Ryan Apr 20 '14 at 08:05
  • What does your JavaScript error console say? – Quentin Apr 20 '14 at 08:21
  • 1
    Chances are high you are running into a [CORS](http://de.wikipedia.org/wiki/Cross-Origin_Resource_Sharing)-issue - as a security measure, websites need to explicitly allow XMLHTTPRequests coming from different domains. Doing as @RPM said is probably your best bet. – Michael Trojanek Apr 20 '14 at 08:22

2 Answers2

1

You should go with a different approach.

Your local JS should do an ajax post to a local URL which will accept the POST method with your json data.

Then your server code(e.g. PHP...) should do an HTTP POST with the data to the remote server, get the response, and send it back to the calling js.

btw, You may send post request like this:

$("button").click(function(){
  var url = "/your-local-server-code.php";
  var data = {"name": "My First Binder"};
  $.post(url, data, function(ans){
      $("p").html( ans);
  });
});

Actually, I recommend you to use jQuery Ajax like this:

$("button").click(function(){
  var url = "/your-local-server-code.php";
  $.ajax({
    type: "POST",
    url: url,
    data: {"name": "My First Binder"},
    success: function(ans){
      $( "p" ).html( ans);
    }
  });
} 

Best Regards.

Dinever
  • 690
  • 5
  • 13
0

You can use jsonp to make the request or your server needs to set to allow origin from api.moxtra.com or "*". Please refer to Cross domain ajax request.

Community
  • 1
  • 1