Solved

jquery getJSON error

Posted on 2013-11-04
3
498 Views
Last Modified: 2013-11-15
I'm running jquery 1.10.2 and this script:

      $.getJSON("http://coldfusion.schupan.com:5001/getprodline?shape=ANG&psc=A&format=json&callback=?", function(json) {
         console.log(json);
      })
     });

This is the json:

{
  "json_list": [
    {
      "descr": "Angle 6061 LBS",
      "prodline": "00A",
      "shape": "ANG",
      "sig": "ALFE"
    },
    {
      "descr": "Angle 6063 LBS",
      "prodline": "00X",
      "shape": "ANG",
      "sig": "ALFE"
    }
  ]
}

I get this error:

"TypeError: this.toArray is not a function"

Why is that?
0
Comment
Question by:kabookis
3 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
due the callback=? you're using JSONP
jQuery will replace the question mark by a random name
the coldfusion server must return the JSON wrapped by parentheses and in first place, at the beggining put this callback parameter value (the random name)

so you should have something like that at the end of your coldfusion code :

<cfset data = serializeJSON(data)>
<cfif structKeyExists(arguments, "callback")>
     <cfset data = arguments.callback & "(" & data & ")">
</cfif>
<cfreturn data>

Open in new window

0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
This looks like a cross-domain request, in which case you need to use JSONP, not just JSON. You'd need to use the $.ajax() function for that, and make sure the server is capable of dishing up the correct JSONP format:

$.ajax({
	dataType: "jsonp",
	url: "http://coldfusion.schupan.com:5001/getprodline?shape=ANG&psc=A&format=json",
	success: function(response){
		console.log(response);
	}
});

Open in new window

0
 

Author Closing Comment

by:kabookis
Comment Utility
OK, I get it now. This script is making a call to a python-generated json URL. In spite of the domain name, there is no CF server involved.
It turned out that all we had to do was install Flask-cors 1.0 in Python, and the request is returned without error as long as the datatype is "json".
The request fails when the datatype is "jsonp".

Sorry for the delay in responding. Thanks for the help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now