TextMarks wants to hear from you

You'll find many helpful knowledge base articles here. If you need assistance, please:

We're here to help! If you're looking for your TextMarks dashboard or more information on TextMarks, please visit our main site at www.TextMarks.com.



Calling TextMarks API from JavaScript

Part of standard JavaScript security as enforced by all standard browsers is something called the "same origin policy". The implication here is that scripts on a page can only make AJAX calls to URLs on the same server from which the page was served. This means that you cannot call TextMarks API directly from JavaScript without a few extra steps.

For more information, see here:
http://en.wikipedia.org/wiki/Same_origin_policy

If you want to make a TextMarks API call from your JavaScript, you can easily work around this limitation in one of the following manners:

Option 1: Using a Proxy

  1. Prepare a script on your server that will receive AJAX calls from your JavaScript. This is allowed because it is the "same origin" (i.e. same hostname in the URL).
  2. Your script will call the TextMarks API in whatever language you are comfortable with (PHP, Python, Java, etc.). It will take its arguments from the URL query params or from whatever is posted to it by your JavaScript, and it will return either the raw TextMarks API response, or it can do further processing and return something simple and specific for your page JavaScript.
  3. Call this new script from JavaScript, which will act as a proxy to call TextMarks API.

Option 2: Using "JSONP"

All TextMarks API requests support an optional parameter "jsonp" to support JSONP (JSON with Padding). See: http://en.wikipedia.org/wiki/JSONP

By settings this parameter, the response JSON will be wrapped in an identically named function call. To use this, you invoke the API call with a <script> tag as follows:

<script type="text/javascript"
         src="http://help.api2.textmarks.com//Test/echo/?str=Hello&jsonp=my_callback">
</script>

These types of requests are limited to GET. For POST requests, please consider using a proxy (option 1).


These guidelines apply not just to using the TextMarks API, but to any web service your JavaScript wishes to consume.