JSON response in Firefox - javascript

I'm making a request in my JS and getting back a json response from the server just fine in Chrome and IE, but for some reason the response I get from firefox isn't working. It looks like it isn't being encoded with UTF-8, but I know that it is from the server and all of my files are as well. The response I get in FF looks like
incoming Text ��[�{�"�m�e�t�a�d...
(I cut of the vast majority of it, but there is that question mark in between every character). The console then says JSON.parse: unexpected character at line 1 column 1 of the JSON data"
My .ajax call is as follows:
var locationURL = "https://myData.com/Metadata.json";
$.support.cors = true;
var jsonData = $.ajax({
url: locationURL,
method: "GET",
dataType: "json",
cache: false,
});
The response header I get back (from firebug) is:
Accept-Ranges: bytes
Access-Control-Allow-Orig...: *
Connection: Keep-Alive
Content-Length: 81950
Content-Type: application/json; charset=utf-8
Date: Mon, 10 Aug 2015 14:03:51 GMT
Etag: "c80b2-1401e-51c80111c1d00"
HSID: Q01-21C D=601
Keep-Alive: timeout=5, max=100
Last-Modified: Tue, 04 Aug 2015 18:00:52 GMT
Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1j
Again, in Chrome and IE the response I'm getting is perfectly valid JSON, without all those extra characters. I see in the console that chrome makes the request to the URL as I have it typed in, but in the firefox console it adds ?_=1439215432794 to the end of the URL, might this have something to do with it? How do I get it to not add that? I put a # at the end of the URL when saving it inside locationURL, but that didn't make a difference. I also tried Jquery's getJSON instead of .ajax but that also didn't help. Any help or direction would be appreciated! Thanks.

Related

Drive Batch API returning text content in addition to json-parsable stuff

My batch process (the creation of some folders) goes just fine, but the response i'm getting back isn't the clean JSON I was expecting but it has plain text headers interspersed. How do I get rid of them or parse this correctly?
--batch__AAPXnCR1-5Q
Content-Type: application/http
Content-ID: response-1
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Wed, 02 Oct 2019 22:44:16 GMT
Expires: Wed, 02 Oct 2019 22:44:16 GMT
Cache-Control: private, max-age=0
Content-Length: 140
{
"kind": "drive#file",
"id": "1pznPt",
"name": "SF1-B1",
"mimeType": "application/vnd.google-apps.folder"
}
--batch_z3tbQ5Q
I just need to be able to extract the id nd nams of these folders after they are created.
How about this answer?
Unfortunately, the response value cannot be directly parsed using the built-in function of Google Apps Script and Javascript. So in my case, I parse the response value using the following script. I think that there are several methods for this situation. So please think of this as just one of several answers.
Sample script 1:
var response = UrlFetchApp.fetch(url, options).getContentText(); // Here, the batch request is run.
var temp = response.split("--batch");
var parsedValue = temp.slice(1, temp.length - 1).map(function(e){return JSON.parse(e.match(/{[\S\s]+}/g)[0])});
Sample script 2:
var response = UrlFetchApp.fetch(url, options).getContentText(); // Here, the batch request is run.
var parsedValue = response.match(/{[\s\S]+?}/g).map(function(e) {return JSON.parse(e)});
If this was not the direction you want, I apologize.

Get data with $resource from Flask API in AngularjJS

I'm using a Flask rest api where I've a method that I call from AngularJS with $resource factory.
I'm using now data to try it, this is the simplified code in the server:
return jsonify({'teacher': 'Tom'})
And in AngularJS, in a controller where I want to get data:
var teacher = $resource('http://localhost:8001/entities/teacher/:id');
teacher.get({id: 1}).$promise.then(function(teacher) {
// success
$scope.teacher = teacher;
}, function(errResponse) {
// fail
console.log('fail')
console.log(errResponse)
});
And this is the log in web browser console:
fail
Object { data: null, status: -1, headers: fd/<(), config: Object, statusText: "" }
But however I can see the request in network console with data in response with 200 status code, because of this I think that I don't know how read this data from $resource response, I've search info about this but I don't know how if I can see the data the resource give mi a fail error.
Any idea?
I know that I should to have this code in a service in another file, it's only a sample to find the solution.
Thanks!
Update:
I think that the problem is in the headers that I received from the server. If I try to use $http with a url that I found as example this work perfect, but when I try the same with my own server this fail even though I can see the response (and it's fine in the network console and it's fine with curl test) I think that the problem is in the headers, maybe $http or $resource needs a specific headers. I' ve updated the original question, I hope that someone can help me. I'm sure that the response is application/json.
This is the test url:
http://jsonplaceholder.typicode.com/users
and the header that is returned is:
Cache-Control
public, max-age=14400
Content-Encoding
gzip
Content-Type
application/json; charset=utf-8
Date
Mon, 03 Oct 2016 07:43:11 GMT
Etag
W/"160d-MxiAGkI3ZBrjm0xiEDfwqw"
Expires
Mon, 03 Oct 2016 11:43:11 GMT
Pragma
no-cache
Server
cloudflare-nginx
Vary
Accept-Encoding
Via
1.1 vegur
X-Firefox-Spdy
h2
X-Powered-By
Express
access-control-allow-credentials
true
cf-cache-status
HIT
cf-ray
2ebec303f9f72f7d-MAD
x-content-type-options
nosniff
And my header is:
Cache-Control
no-cache
Content-Length
35
Content-Type
application/json
Date
Mon, 03 Oct 2016 08:26:00 GMT
Expires
Fri, 01 Jan 1990 00:00:00 GMT
Link
Server
Development/2.0
I'm using a Google App Engine developer Server (I don't know if this is relevant).

json default charset API and browser display

My api returns following json.
HTTP/1.1 200 OK
Date: Fri, 30 Aug 2013 14:14:32 GMT
Server: Apache/2.2.22 (Ubuntu)
Content-Length: 431
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json
{
"member": [
{
"city": "Li\u00e8ge",
"country": "Belgi\u00eb",
}
]
}
following the specs ( JSON specs )
JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
So the Contect-Type is not specifying the charset
But chrome or IE10 browsers are not displaying the è ( \u00e8 ) and ë ( \u00eb ) correct.
Is this because chrome or IE10 have a different default charset . I need to give the API to a customer and he will also see the strings "Belgi\u00eb"and "Li\u00e8ge". Am i violating the specs somehow or am i doing it right and is the browser beeing stupid?
You can specify the charset if you want:
contentType : 'application/json; charset=utf-8'
And also use utf8_decode (or similar) at the other end just ot make sure.
Works here.

How to get final destination URL from AJAX?

When I do an XMLHttpRequest, I always get redirected automatically to the URL (presumably by the headers of the response). For example, if I query "http://www.stackoverflow.com" I will be redirected to "http://stackoverflow.com".
How can I get that final URL? (http://stackoverflow.com/ in the example)
I checked in the response headers but I cannot seem to find it. (I just used the GET/POST method not HEAD).
Look for a location header in the response.
In the example you gave, accessing www.stackoverflow.com and being redirected to stackoverflow.com here is most definatly a Location header being used.
[trcjr#rigel ~]$ curl -I http://www.stackoverflow.com
HTTP/1.1 301 Moved Permanently
Content-Length: 148
Content-Type: text/html; charset=UTF-8
Location: http://stackoverflow.com/
Date: Sat, 05 Feb 2011 21:47:17 GMT
[trcjr#rigel ~]$
Using jQuery (this is for current page url) :
$(document).ready(function () {
var href = window.location.href.toString();
});
EDIT : For, response page final url, pass jqXHR to ajaxComplete & then read the header.

Ajax response is gzip compressed - Prototype, Firefox can't handle it

I'm trying to query a web service (with JavaScript, prototype). The server responds with XML, but compresses it; headers are set appropriately.
Under Safari 4, everything is fine. The response is decompressed and JavaScript can deal with the data.
Under Firefox 3.5.8, no data is returned to JavaScript!
Code:
var req = new Ajax.Request(this.url, {
asynchronous: false,
contentType: 'text/xml',
method: 'post',
postBody: xmlString,
onSuccess: function(t) {
// debug, place response into textarea to show
$('responseText').value = t.responseText;
}
});
This is the response, as I trace it on the network:
HTTP/1.1 200 OK.
Date: Fri, 05 Mar 2010 14:10:51 GMT.
Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny6 with Suhosin-Patch.
X-Powered-By: PHP/5.2.6-1+lenny6.
Vary: Accept-Encoding.
Content-Encoding: gzip.
Content-Length: 2104.
Keep-Alive: timeout=15, max=100.
Connection: Keep-Alive.
Content-Type: text/xml.
.
............]s......W`.3...H&A.$.Q.^[.:....... (and so on ...)
Any idea why this is happening? What can I do about it?
I tried setting the 'Accept-Encoding' header in the request, can't get it working properly. Besides, the response can be rather large, meaning: it's good that it is compressed by the server.

Categories

Resources