Solved

jquery getJSON error

Posted on 2013-11-04
3
527 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
[X]
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
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39621818
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 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39621821
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
ID: 39652145
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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)

737 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