2

I'm relatively new to AJAX and API calls and I'm working on a POST API call to a company called idibu that handles job postings. It requires POST variables (not GET variables) so I'm using a AJAX with a type of 'POST.' The POST data I'm using is from the idibu documentation as a working example for an API call (it's encoded in iso-8859-1, which is required, and can be read about here: http://www.idibuworld.com/docs/xml-api-v3-examples). There is a 'hash key' or API key that we've been provided but I took out of the code here (just letting you know that I've entered it correctly in my code). Here's my code:

<script>
$('.api_test').click(function(){

    var myData =
    'xml_text=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0D%0A%3Cidibu%3E%0D%0A%3Cmethod%3Eadd%3C%2Fmethod%3E%0D%0A%3Cconfig%3E%0D%0A%3Cshow_durations%3Eno%3C%2Fshow_durations%3E%0D%0A%3Ccompletionurl%3Eemail%3C%2Fcompletionurl%3E%0D%0A%3Cadvertcompletionemail%3Ebob%40bob.com%3C%2Fadvertcompletionemail%3E%0D%0A%3Clockboards%3Eyes%3C%2Flockboards%3E%0D%0A%3Credirecturl%3Ehttp%3A%2F%2Fwww.google.com+%3C%2Fredirecturl%3E%0D%0A%3Cvalidate_level%3Ewarning%3C%2Fvalidate_level%3E%0D%0A%3C%2Fconfig%3E%0D%0A%3Cjob%3E%0D%0A%3Ctitle%3E%3C%21%5BCDATA%5BXML+v+3+test%2C+please+ignore+Special+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+-+%3A+%3B+_+%2B+%27+%22+%80%5D%5D%3E%3C%2Ftitle%3E%0D%0A%3Creference%3EABC123456789%3C%2Freference%3E%0D%0A%3Cdescription%3E%3C%21%5BCDATA%5B%3Cb%3ESpecial+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+here+euro+%80+%0D%0ABasic+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0ASpecial+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0A%0D%0A+%0D%0A+Special+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0ABasic+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0A%0D%0A+%96+%96+%92+%27+%95+++++++%2C+%09%2C+%95+%B7+%93+%94+%D8+%85+%A8+%A6+%27+%3A+%27+%83+%E1+%91+%BA+%A1+%A7+%E9+%D7+%87+%27+%3A+%27+%27+%C7+%BD+%84+h+%AE+%97+%AC+%83+%E1+%99%3C%2Fb%3E%5D%5D%3E%3C%2Fdescription%3E%0D%0A%3Csender%3E%0D%0A%3Cname%3ESteve%3C%2Fname%3E%0D%0A%3Clastname%3ERogers%3C%2Flastname%3E%0D%0A%3Cemail%3Etest%40mail.net%3C%2Femail%3E%0D%0A%3Ccompany%3EOne+World+Market%3C%2Fcompany%3E%0D%0A%3Cphone%3E44+%280%29+111-1111111%3C%2Fphone%3E%0D%0A%3Cwww%3Ehttp%3A%2F%2Fuk.idibu.com+%3C%2Fwww%3E%0D%0A%3Ccountry%3EUK%3C%2Fcountry%3E%0D%0A%3Cpostcode%3E020+1111+1111%3C%2Fpostcode%3E%0D%0A%3C%2Fsender%3E%0D%0A%3Ccategory+id%3D%2221%22+%2F%3E%0D%0A%3Clocation+id%3D%228%22+%2F%3E%0D%0A%3Csublocation+id%3D%22668%22+%2F%3E%0D%0A%3Cjobtype+id%3D%222%22+%2F%3E%0D%0A%3Cjob_time%3E2%3C%2Fjob_time%3E%0D%0A%3Cstartdate%3E2011-11-26%3C%2Fstartdate%3E%0D%0A%3Cduration%3EFull+time%3C%2Fduration%3E%0D%0A%3Csalarymin%3E20000%3C%2Fsalarymin%3E%0D%0A%3Csalarymax%3E25000%3C%2Fsalarymax%3E%0D%0A%3Csalaryper+value%3D%22annum%22+%2F%3E%0D%0A%3CsalaryOverride%3ESalary+override+test+test%3C%2FsalaryOverride%3E%0D%0A%3Ccurrency%3EGBP%3C%2Fcurrency%3E%0D%0A%3Cpublish%3E2011-11-26%3C%2Fpublish%3E%0D%0A%3Cposts%3E%0D%0A%3Cboard+id%3D%22517%22%3E%0D%0A%3Cextrafield+name%3D%22idibudts_cat%22%3E3%3C%2Fextrafield%3E%0D%0A%3Cduration+days%3D%227%22+%2F%3E%0D%0A%3C%2Fboard%3E%0D%0A%3C%2Fposts%3E%0D%0A%3C%2Fjob%3E%0D%0A%3C%2Fidibu%3E';

    $.ajax({
        type: 'POST', // HTTP method POST or GET
        url: 'http://ws.idibu.com/clients/api/REMOTE/V3/[INSERT HASH KEY]', //Where to make Ajax calls
        dataType:'xml', // Data type, HTML, json etc.
        async:false,
        data:myData, //post variables
        contentType: "text/xml; charset=\"utf-8\"",
        success:function(response){

            $("#preForXMLResponse").html('<pre>'+response+'</pre>');

        },
        error:function (xhr, ajaxOptions, thrownError){
            alert('didn\'t work'); //throw any errors
        }
    });

});
</script>

Now, I'm not sure if the problem has anything to do with the actual 'data' being passed because it's giving me a the error on the AJAX call (it alerts out 'didn't work') meaning it never posted to anywhere.

Like I said, I'm new to this and have been reading up on how to send POST calls (and I have sent successful AJAX calls in the past on other projects). There's more documentation on idibu here (http://www.idibuworld.com/docs/submitting-and-responses), though I'm not sure if it's just a syntax error on my end. There's not great support yet for their API so I'm just trying to sort through it as best I can.

Thanks for help!

EDIT

I get these errors on the console when I try to submit (again, I'm editing the code for privacy purposes but it's displaying my URL and HASH KEY in the actual errors):

Failed to load resource: Origin http://[MY_SITE] is not allowed by Access-Control-Allow-Origin. http://ws.idibu.com/clients/api/REMOTE/V3/[INSERT HASH KEY]

XMLHttpRequest cannot load http://ws.idibu.com/clients/api/REMOTE/V3/[INSERT HASH KEY]. Origin http://[MY_SITE] is not allowed by Access-Control-Allow-Origin. 

In this question (Origin 'url' is not allowed by Access-Control-Allow-Origin), they mentioned that you can't make AJAX calls to sites outside of your server. Am I approaching this POST method incorrectly? This is on a PHP page so would you all suggest cURL (I'm not well-versed in it which is why I attempted jQuery AJAX)?

Community
  • 1
  • 1
MillerMedia
  • 3,651
  • 17
  • 71
  • 150
  • You should check the error on your browser's network tab (that must be on javascript console). – Okan Kocyigit Jul 06 '13 at 00:53
  • @ocanal, thanks for that. I've updated my question with those errors. – MillerMedia Jul 06 '13 at 01:12
  • I did find this article: http://stackoverflow.com/questions/10143093/origin-is-not-allowed-by-access-control-allow-origin that may have a clue in it... – MillerMedia Jul 06 '13 at 01:13
  • Those links that you've posted requires authentication. Have you tried to load that url using GET request? Just for debugging purpose I would try it in a browser with your hash key. The reason I say this is because I want to assume people accepting `POST` would at least do it over `https`. – vee Jul 06 '13 at 02:29
  • @vinodadhikary The GET request is not supported with the API. In their documentation, they say that it has to be POST. I suppose that's just the way it's set up? – MillerMedia Jul 06 '13 at 04:20

1 Answers1

1

An answer after your edit that contains the ERROR information,

Yes, it's impossible to make a request via ajax from another domain that is not allowed by the requested domain on client side.

There are some methods to achieve this problem,

JSONP

I can't access the idibu's API, so I have no idea whether that supports JSONP. You should check the docs and find if it does.

CORS

You can't use this one, because in this method the idibu API must allow your domain on their side. I wouldn't pass this off without mention.

cURL

As you mentioned in your edit, If you are able to make this request via cURL, just do it. There are no restrictions like Access-Control-Allow-Origin.

Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129