natecraft1
natecraft1

Reputation: 2836

sending html in JSONP request

I'm trying to make a request here https://s3.amazonaws.com/lawkickstas/lawkick_html.json for this...

{"html":"<div id=\'nates_widget\'>
  <div id=\'question\'></div>
  <form>
    <input type=\'text\' />
  </form>
  <div id=\'result\'></div>
  <div id=\'hints\'></div>
</div>"}

with this

$.getJSON("https://s3.amazonaws.com/lawkickstas/lawkick_html.json?callback=?",   function(result){
 //response data are now in the result variable
 alert(result);
});

but i'm getting an error unexpected token :
I'm wondering A. if this is the way to go about retrieving html from an external source and B. What is this callback and where do I put it?

Upvotes: 0

Views: 130

Answers (2)

msapkal
msapkal

Reputation: 8346

@natecraft, you could also use $.getJSON

$.getJSON("https://s3.amazonaws.com/lawkickstas/lawkick_html.json?callback=?",function(result){
});
function myJsonMethod(str) { console.log(str); }

Upvotes: 1

natecraft1
natecraft1

Reputation: 2836

Here's what worked after many attempts. In the .json file I was making a request to I changed it to this (wrapped it in a function)...

myJsonMethod({"html": "<div id='nates_widget'><div id='question'></div><form><input type='text' /></form><div id='result'></div><div id='hints'></div></div>"})

then to make the call...

$.ajax({
  type : "GET",
  url :"https://s3.amazonaws.com/lawkickstas/lawkick_html.json?callback=?",
  dataType :"jsonp",
  jsonp: false,
  jsonpCallback: "myJsonMethod",
  success : function(data){
     console.log(data, "WORKED");},
  error : function(httpReq,status,exception){
    alert(status+" "+exception);
 }
});

It did not work when the object in the json file was not surrounded by myJsonMethod() and also did not work when the line jsonpCallback: "myJsonMethod" was not included. Why? God knows.

Upvotes: 0

Related Questions