api connect

Posted on 2014-12-26
Last Modified: 2014-12-28
Yes I am trying to learn to connect to a api, every way I try does not seem to connect using jquery. Np with angular but I need to connect with jquery to retrieve the value.
Question by:sevensnake77
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 110

Accepted Solution

Ray Paseur earned 250 total points
ID: 40519734
There might be a couple of issues here.  You can use Chrome Developer Tools to find out about the issues.

Here is one problem that I encountered:
XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.
To get around that, I copied the API response to my server at this URL:

Next I used JavaScript alert() to visualize the data.  I could see that I was getting the JSON string this time.  So I added the jQuery.parseJSON() method to the script and was able to get it to work.

Going forward, you might find that you need JSONP to handle the cross-origin request.

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>E-E Q_28587196</title>

<script type="text/javascript" src=""></script>
    $.get("", function(data) {
	    var jobj = $.parseJSON(data);


<p class="my-id">The ID is </p>
<p class="my-content">The content is </p>


Open in new window

HTH, ~Ray
LVL 82

Assisted Solution

leakim971 earned 125 total points
ID: 40519741
Test page :

More info :

$(document).ready(function() {
    var api_endpoint = "";
    window.mycallback = function(data) {
        url: "" + encodeURIComponent(api_endpoint)

Open in new window

LVL 58

Assisted Solution

Gary earned 125 total points
ID: 40520087
Forget the first junk comment here - not living in the real world or answering the real question of a cross domain request - hey I totally ignored this is for a different domain so just copied the reponse to my server so it worked - jaysus!

Here's leakim's fiddle working (who actually made a real world working sample)

But that relies on a third party to deliver you the results which is not an ideal situation

Because you are hitting the cross domain policy and fails with json requests to the URL your best best is to use a proxy

That means you make a request to your server with the UPC number and your server makes the request to and returns the data to the browser.

This has the benefit of not exposing your API key which is a bad thing to make public since anyone can use it.

So your ajax, instead of making a direct request to, makes a request to your server passing the UPC number - lets say the page is called process_upc.php
Then in process_upc.php use the sample code here to make the real request to and then just echo out the results

Your code as it is in the fiddle will remain the same, the only difference will be the url
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40520415
Gary, as I wrote right at the top, "There might be a couple of issues here."  One of the issues was a cross-domain request.  That was solved by this script (which also enabled us to hide the API key).
<?php // temp_sevensnake.php

 * JSON: {"valid":"true"
 *       ,"number":"077633047375"
 *       ,"itemname":"SUNBEAM TEXAS GAINT"
 *       ,"alias":""
 *       ,"description":"SUNBEAM TEXAS GAINT"
 *       ,"avg_price":""
 *       ,"rate_up":0
 *       ,"rate_down":0
 *       }

$url = '';

$get = !empty($_GET['q']) ? $_GET['q'] : '077633047375';
echo file_get_contents($url . $get);

Open in new window

The other issue was that the jQuery script did not parse the JSON, so it did not have access to the JSON object properties.  I think it's OK for us to overcome the issues one step at a time, and it's also OK to show the author of the question the logic and steps we take as we try to help debug something.

Anyway, I'll sign off on this question now, since I think it's adequately answered and explained.

Author Closing Comment

ID: 40521295
Thanks, I gave ray the best solution because I seen he done it in php and that gave me and idea, so I done it in C# But thanks again.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The most up-to-date version of this article is on my Blog
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question