I'm getting a malformed JSON response from an AJAX $.getJSON() request. I don't understand the problem.
Here is the request code:
var myfunc = function(){
$.getJSON( "/", {"data": ""}, function( data, status ){
var values = data;
$("#temperature").html( values.temperature.toFixed(1).toString() );
$("#humidity").html( values.humidity.toFixed(0).toString() );
});
});
and here is the JSON data received (extracted via Firefox debugger):
{
"temperature": 17.799999237060547,
"humidity": 35.900001525878906,
"failed": false
}
I cannot see what is malformed here. And the code works. DOM elements id="temperature" and id="humidity" are updated correctly.
I got exactly the same result using $.get() with JSON.parse().
Does anybody have an idea how to solve the problem?
My guess is that the json data you are receiving over the network is malformed, but, it is successfully converted to an object anyway.
getJSON automatically applies JSON.parse(..) on the received data.
Try using the 'network' listener tab on Google Chrome to see exactly the response you are receiving BEFORE it is parsed. There might be a missing " or something like that.
If you have access to the server code, you could also try logging the response in there.
edit:
you might be interested by this link
Might have to do with some server config.
Mimetype is also mentionned in this link.
Related
I am building a form using Polymer 1.0, using its iron-form element.
How can I get the error response body? The server is sending a JSON error back, and I can see it in the chrome inspector (network tab). But when my iron-form-error callback method is called, and I log it, the response JSON is nowhere to be found. I went through all the Object properties but nothing.
I also looked under event.detail.request.response but it is also null. Here is the code I'm using:
document.querySelector('#formPut').addEventListener('iron-form-error',function(e){
console.log(e);
// var json = $.parseJSON(e.detail.error.message);
});
Any help would be appreciated.
I eventually found my response body here...
e.detail.request.xhr.response
For those looking for the same thing under Polymer 1.0+, the status code can now be found under event.detail.request.xhr.status
_onError: function(event) {
statusCode = event.detail.request.xhr.status;
}
I'm trying to do a getJSON call to a page within my domain that is simply a json array, which is not returning anything given the script in my webpage:
<script>
$('#genButton').click(function() {
console.log("click");
$.getJSON("http://mygithub.github.io/repository/quotedb.json", function(data){
console.log("executing");
var entry = data[Math.floor(Math.random()*data.length)];
console.log(entry);
$("#quoteDisplay").text(entry);
}).fail(function() {
console.log( "error" );
});
})
</script>
When the button is clicked, the console logs the "click", but never logs "executing", but also doesn't log any errors related to the getJSON. The debugger also never pauses within the getJSON function.
My question is, 1. Is it valid for quotedb.json to be literally a webpage with just a single json array? Or does the page need to be annotated in some way so that the getJSON request knows where to pick up the json from? 2. If (1) is fine, then are there any other reasons you can see why nothing from the getJSON return function would appear?
The json is again one array of json objects, in the form:
{
"character": ""
"quote": ""
}
EDITS
With the edited code, it now outputs "click" followed by "error" from the .fail() method of the $.getJSON call now.
I tried removing the ?callback? section of the url as suggested below, which did not work.
The /repository/ section of my url seems to be necessary to access the file.
Try without ?callback=? at end of url, see jQuery.getJSON()
JSONP
If the URL includes the string "callback=?" (or similar, as defined by
the server-side API), the request is treated as JSONP instead. See the
discussion of the jsonp data type in $.ajax() for more details.
I am using dojo/request/iframe to do file upload. The file is uploaded fine, but when I am trying to get the response in XML, I always get this error (I tried in FF):
"TypeError: xmlText is undefined"
I have set the handleAs property to "xml" and based on the documentation of dojo/request/iframe, for responses in xml and html I don't need the response to be inside tag. I used Firebug and I can see that my response is in xml format.
I have read a post about the possibility of this become a defect from dojo library, but there's no follow up on this: http://grokbase.com/p/dojo/dojo-checkins/12c7qyynnx/dojo-toolkit-16440-dojo-request-iframe-js-xmltext-is-undefined-error-in-ff-and-ie9
Does anybody know if this is actually a defect from Dojo? and is there any workaround? Thanks in advance
I had a similar problem. But maybe it's misleading: The response, your xmlText is no Text, but an object. For me something like
iframe(myForm.action, {
handleAs: "html",
data: data
}).then(function(xmldoc){
require(["dojo/dom-construct"],function(domConst){
var new_elem = query("#"+myid,xmldoc)[0];
if(new_elem)
domConst.place(new_elem, elem, "replace");
});
}, function(err){
// Handle the error condition
});
did work fine.
If you copy and past the following url to your browser:
http://finance.google.com/finance/info?client=ig&q=MUTF_CA%3ATDB900
it will output an string no problem. (that is what i wanted to retrieve from the following ajax call)
But if i do the following:
this.getQuote = function() {
$.get('http://finance.google.com/finance/info?client=ig&q=MUTF_CA%3ATDB900', callback);
}
var callback = function(data){
alert(data);
}
It gave me an "500 Internal Server Error". I checked using firebug console.
Did i do something wrong in the ajax call?
Thanks.
As Shadow_boi already guessed, the problem is due to the same origin policiy, which always applies to ajax requests. You need to use JSONP to fix the problem.
See this fiddle for solution: http://jsfiddle.net/cb9c3/
I had a problem with jquery's post api.
$(".MGdi").click(function () {
id=$(this).attr("rel")
$.post( 'Mdeger.asp?cmd=MG', { id: id, drm: $(this).html()} ,
function( data ) {
var $response=$(data);
var snc = $response.find('#snc').html();
alert(snc);
},"application/x-www-form-urlencoded");
});
Another way is:
$(".Pasif").click(function () {
id=$(this).attr("rel")
$.post( 'Mdeger.asp?cmd=Pasif', { id: id, drm: $(this).html()} ,
function( data ) {
$(this).html(data);
alert(data)
},"application/x-www-form-urlencoded");
});
Everything is OK on serverside but clientside's success function does nothing.
Even basic codes like alert("hoho"); success not triggering.
this usually happens when respond couldn't be parsed. you should check the respond using firebug or similar debugging tool.
especially the methods that expects json data, strictly validates the respond and if there is anything invalid it just does nothing, no-error, no-warning, no-exception.
when your callback function doesn't run, you should suspect that your respond isn't correct.
// Türkçe özet
uzun lafın kısası dönüş değerinde bir terslik varsa dönüş fonksiyonu çalışmayacaktır. sunucudan gelen değerleri iyice kontrol etmekte fayda var. jquery dönüş değerinde veya dönüş fonksiyonunda bir hata olursa seni uyarmadan işi sonlandırıyor.
I had this problem as well. It turns out I was making an AJAX call to the same domain, but on a different port, which is not allowed (for security reasons) in Javascript.
See this relevant question for more info:
How do I send an AJAX request on a different port with jQuery?
I was very surprised that the AJAX call would POST/GET to the server, (which I was able to verify by looking at the server log) but that the response was never read. I would have thought that both sending and receiving would be disallowed.
I had this error too, and that was a stupid problem : I set dataType to "json" in my JS, but the page called was returning plain HTML. And this cause to not fire the success function at all.