Blogs

Set Up JQuery to Interact with the Smart Publish API

by User Not Found | Feb 06, 2012

What did we ever do before JQuery came about?  Back in the day, when we wanted to use AJAX to interact with services from the browser using javascript, we had to detect which browser was being used and use the appropriate objects to call the services. We then had to write browser specific code to update the UI since there were subtle differences in how to perform DOM manipulations in code.  Thanks to JQuery, all of those nuances have been abstracted and we can let the library take care of those subtleties. Here are some tips in getting JQuery to work for you when you want to call our Smart Publish API (SPA) services.  

At this point you should be signed up for our services and have set up an application through our Partner Workspace. Each application will have it's own APIKey which you will need to interact with our services.  The APIkey is passed in the header of each call to the SPA Services. Without this header, the service will return a "403 Forbidden" http code. 

JQuery makes it easy to set up default values for all ajax interactions by using the $.ajaxSetup function and we'll take advantage of that to make sure the APIKey is added to all calls.  In this example, we'll create a javascript function that  sets the request header value for Authorization and then in the $(document).ready function, we'll set the default ajax values. Don't forget to replace YOURAPIKEYHERE with the APIKey that you get from the Application Management Partner Workspace page (you'll need to be logged in to access that page). 

 

function AddAuthHeaders(xhr, settings) {

    xhr.setRequestHeader('Authorization', 'apikey=YOURAPIKEYHERE');
     //perform any other header updates you need here

}
 
$(document).ready(function () {
    //set default values for all of the service calls
    $.ajaxSetup({
        contentType: "application/json",
        dataType: "json",
        beforeSend: AddAuthHeaders
    });
}

 

Alright, we've gotten that authorization header out of the way, now we need to tell JQuery that it's ok to make cross origin scripting requests since the services are not hosted at the same URL as the web site.  Some browsers throw errors and some don't (Internet Explorer will complain with a "No Transport" error if you don't do this), so we can generally handle this by adding the following in the $(document).ready function:

 

jQuery.support.cors = true;

 

One last bit of housekeeping to take care of and that's to make sure the protocol of your AJAX requests are the same as the containing web page.  Usually what I do is keep a global javascript variable that contains the root of the RESTful requests including whether they should be called through http or https. I'll usually define the value in the $(document).ready by getting the window.location.protocol value and appending it to a constant for the service root URI. The SPA API requires all calls coming in to be through https, so at the point you're making the SPA calls, your page should be at an https URL.  Putting it all together, the $(document).ready function looks like this:

 

//defined up here so that they have global scope
var apiKey = "YOURAPIKEYHERE";
var serviceRootURI = "preview.webservices.fujifilmesys.com/spa/v2/"; //test URI
 
function AddAuthHeaders(xhr, settings) {
    xhr.setRequestHeader('Authorization', 'apikey=' + apiKey);
    //do any other header adds here
}
 
$(document).ready(function () {
    //set default values for all of the service calls
    $.ajaxSetup({
        contentType: "application/json",
        dataType: "json",
        beforeSend: AddAuthHeaders
    });
 
    jQuery.support.cors = true;
    serviceRootURI = window.location.protocol + "//" + serviceRootURI;
 
    //setup is complete, do other stuff now :)
});

With this code in the JQuery $(document).ready function and the AddAuthHeaders helper function, we're ready to start making calls to the Smart Publish API. The next blog entry will look at the use of our JQuery library for interacting with the individual services. Until then, feel free to leave us a comment to let me know what you think!

21 Comments

  1. 1 ppsspp gold apk 16 Apr
    ppsspp gold apk available here  
  2. 2 marathi dj songs 16 Apr
  3. 3 tutu app pokemon go 06 Apr
    Tutu app Pokemon go is a Chinese origin application which is a free application for android and ios users. This application was developed by Chinese developers to make the games better as compared to their available versions. 
    <a href="http://tutuapps.org/tutuapp-pokemon-go-hack-download/"/>tutu app Pokemon go</a>

  4. 4 Tutu app Pokemon go 06 Apr
    Tutu app Pokemon go is a Chinese origin application which is a free application for android and ios users. This application was developed by Chinese developers to make the games better as compared to their available versions. 
    <a href="http://tutuapps.org/tutuapp-pokemon-go-hack-download/"/>tutu app pokemon go</a>
  5. 5 mpresults.nic.in 05 Apr
    www.mpresults.nic.in : we are going to provide you the information regarding MP Board 10th Result 2017 
    mpresults.nic.in

  6. 6 Robert 29 Mar
    Developers are allowing us to get at their data much more Money Converter transparently now, never again is everything safety secured. Sites are pleased to have you get their information and actually.
  7. 7 Jack kevin 22 Mar
    Engineers are permitting us to get at information substantially more Custom Assignment Help straightforwardly now, never again is everything safety guarded. Sites are pleased to have get to their information and, truth be told, energize it.
  8. 8 Joseph Dahlgren 20 Mar
    Hi! I really sympathize with your situation. I'm going through the same thing right now and have looked into the manga industry and found opportunities for manga artists to publish their own original titles are quite limited. Yen Press is the only prominent publisher working on OEL titles right now and they are largely concentrating on adapting properties with already built in audiences. James Patterson and other YA books like the Parasol Protectorate series. My suggestion is to definitely launch it as a webcomic, build a following cheap custom essay online and self-publish the work yourself. Yes, indeed, it is a lot of hard work but trust me when I say this hard work pays off. You will come to understand the publishing process intimately, will have more control over the way your book is presented and do marketing on your own terms using Facebook, Twitter. Understanding what goes into a successful launch of a book better equips you to approach a publisher down the road. As a beginner, you don't have enough clout to negotiate terms with them unless 1 You are up to your ears in talent or 2 you already have a strong following through other means via Deviantart etc.
  9. 9 swiggy hyderabad offers 23 Feb
    Thanks for the post
  10. 10 best gaming mouse under 50 04 Feb
    <a href="https://bestgamingmouse2017.org/">best gaming mouse</a>


  11. 11 Bishop consultancy 03 Feb
    I have known about JQuery yet don't comprehend what it does. Like what does it really JQuery convey to a site? For instance, i need a live webcam talk on my assignment help UK site. In the event that i utilize JQuery by what means will it help me?
  12. 12 jimmy 22 Jan
    http://apkspeed.com visit now if you want to download best apk apps
  13. 13 James Cameron 19 Jan
    The agenda utilize JavaScript, we wanted to know which plan was being Buy Assignment utilize the appropriate substance to call the administration. We then required composing agenda exacting system to overhaul.
  14. 14 download Kik messenger for laptop 10 Jan
    Kik for PC on Windows 10/8.1/8/7. download Kik messenger for laptop & MAC using Bluestacks & Android.
  15. 15 How To Write A Research Paper 06 Jan
    JSON-based APIs are a hot property on the Web at this moment. I can't recall the last time I went onto a blog or portfolio without seeing the proprietor's tweets or Facebook companions How To Write A Research Paper gazing back at me. This intuitiveness makes the Web an energizing spot. As far as possible is by all accounts individuals' creative energy. As exhibited by everything from pulled tweets to a mindful trade rates API, information is at present ruler, and sites are swapping it uninhibitedly.
  16. 16 llya 15 Dec

    <a href="http://www.itFFFF.com/">bgfgfhsg</a>[url=http://www.itFFFF.com/]wendyouyt[/url]
  17. 17 SBI UPI App 09 Dec
    Learn How to download SBI UPI App. You can easily transfer money without using the bank account details of the payee through the SBI Pay App.
  18. 18 New Year 2017 Images 08 Dec
    That’s why people are looking for the New Year Wishes, quotes and messagesThat’s why people are looking for the New Year Wishes, quotes and messages


  19. 19 OnePlus 5 Specs 06 Dec
    Here's everything we know about the OnePlus 4. Get Release Date, Price, Features, Rumors and Specifications of OnePlus 4.
    OnePlus 5 Specs

  20. 20 agjsgf 16 Nov
    Coaching makes the pre-adult careful. In custom dissertation writing services countries sex preparing has amazing congruity. Sex preparing is extremely helpful to supervise secondary school pregnancy. Sex guideline moreover offers information to sexually sent ailments.
  21. 21 dumpy 24 May
    great

Comment

  1.