Move this topic
$.getJSON() Usage
in Getting Started
•
10 years ago
I'm trying to use the ".getJSON()" method for the first time and so far I'm not getting anything out of it. I know the callback function for it is supposed to be called when the call succeeds, but can I also code an error callback function for it, or instead of doing that should I just use the more general ajax method and then code callback functions for both success and error, and make the dataType JSON? Thanks for the help.
1
Replies(13)
So instead of continuing with using $.getJSON() I tried using the AJAX method as shown coded below:
${esc.d}.ajax( { url: targetUrl, type: "GET", dataType: "JSON",
success: function(data)
{
alert("GOT JSON CALL SUCCESS RESPONSE!...");
},
error: function(jqXHR,textStatus,errorThrown)
{
alert("GOT JSON CALL ERROR RESPONSE..jqXHR="+jqXHR+" TEXT-STATUS="+textStatus+" ERROR-THROWN="+errorThrown+"!...");
}
});
success: function(data)
{
alert("GOT JSON CALL SUCCESS RESPONSE!...");
},
error: function(jqXHR,textStatus,errorThrown)
{
alert("GOT JSON CALL ERROR RESPONSE..jqXHR="+jqXHR+" TEXT-STATUS="+textStatus+" ERROR-THROWN="+errorThrown+"!...");
}
});
I got the following data back from the above coded "error" callback:
jqXHR=[object Object] TEXT-STATUS=parsererror ERROR-THROWN=SyntaxError: JSON.parse: unexpected character
did I get this error because I shouldn't have even coded the "error" callback for an "ajax()" method call (according to the documentation) or because there actually was a problem with what I'm passing to the "ajax()" method? I'm confused. Can someone pleas explain where my problem is?
Thanks.
Leave a comment on n0vice.1's reply
Leave a comment on Kevin B's reply
Re: $.getJSON() Usage
10 years ago
Is there anyway to further determine what the problem may be by somehow looking at the raw data coming back? I've also looked for "parse error", "syntax error", and "unexpected character", all from the error data returned, in the documentation without finding anything useful. So do I need to be looking for those things in JQuery or in Javascript?
Thanks for the help.
Leave a comment on n0vice.1's reply
Re: $.getJSON() Usage
10 years ago
navigate to the url you are requesting from directly using your browser, and paste the results here
the error you are getting basically means your JSON is invalid. Therefore, your problem is neither in javascript nor jQuery.
-- Kevin
the error you are getting basically means your JSON is invalid. Therefore, your problem is neither in javascript nor jQuery.
-- Kevin
Leave a comment on Kevin B's reply
Leave a comment on n0vice.1's reply
Re: $.getJSON() Usage
10 years ago
OK. Here's the part the response html from the original target page which my proxy page is supposed to be bringing back:
<
table width="100%">
<
div id="detailData" >
<
tr>
<
td valign="top"class="smallfont">
01:00 PM-02:00 PM
<br>ACE Center
<
input class="research" type="hidden" value="201057"/>
</
td>
<
td valign="top"class="smallfont">
01:00 PM-02:00 PM
<br>ACE Center
<
input class="research" type="hidden" value="201057"/>
</
td>
<
td valign="top"class="smallfont">
01:00 PM-02:00 PM
<br>ACE Center
<
input class="research" type="hidden" value="201057"/>
</
td>
<
td valign="top"class="smallfont">
01:00 PM-02:00 PM
<br>ACE Center
<
input class="research" type="hidden" value="201057"/>
</
td>
<
td valign="top"class="smallfont">
</
td>
<
td valign="top" class="smallfont">
</
td>
</
tr>
</
div>
</
table>
Though there is a lot more html coded on the page besides this. Hope this helps.
Thanks.
Leave a comment on n0vice.1's reply
Re: $.getJSON() Usage
10 years ago
well, your biggest problem is that HTML != JSON. your dataType: "json" should be dataType: "html" or "text"
-- Kevin
-- Kevin
Leave a comment on Kevin B's reply
Re: $.getJSON() Usage
10 years ago
Next, the HTML you are getting is invalid, a <table> element cannot contain a <div> as a direct child.
-- Kevin
-- Kevin
Leave a comment on Kevin B's reply
Re: $.getJSON() Usage
10 years ago
OK. Then I'm confused at how I can retrieve the data I want and have it returned in some kind of JSON object. I've been successful previously at using the ".text()" and ".html()" methods for bringing back stuff. This is my first attempt at trying to bring back something in a JSON object. So what do I need to do to successfull bring it back in a JSON object?
Leave a comment on n0vice.1's reply
Re: $.getJSON() Usage
10 years ago
The server has to return it as JSON. Otherwise there isn't anything you can do to make it json.
-- Kevin
-- Kevin
Leave a comment on Kevin B's reply
Re: $.getJSON() Usage
10 years ago
The environment I'm in is a dotcms (content management system) type of website. We use Velocity for writing the server code both for making the actual queries to the database and for formatting the returned data into standard html on a webpage which can be viewed. The original data is returned in list format but at this point I'm unsure how I could manipulate that to be returned in some sort of JSON object. any thoughts?
Leave a comment on n0vice.1's reply
Re: $.getJSON() Usage
10 years ago
All i can say is it would need to be done by server-side code. I'm not familiar with the server-side application you are using.
-- Kevin
-- Kevin
Leave a comment on Kevin B's reply
Re: $.getJSON() Usage
10 years ago
Thank you for the help nonetheless. I have posted a question to that effect on another forum for dotcms which I'm hoping will eventually return some kind of satisfactory answer to it. Thanks again.
Leave a comment on n0vice.1's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic
Reply to n0vice.1's question