I don't see anything wrong with your code. You must have some other
problem.
But you don't need to "search" and you don't
need the variable mType in any case.
-
var
fileExt = 'docx';
-
var
mimeType = getMimeType(fileExt);
-
-
function
getMimeType(ext) {
-
$getJSON('json/mimeTypes.json', function(data)
{
-
return data[ext];
-
)};
-
}
I changed the
name of the function parameter above just to avoid confusing
anybody who might think that the the function is accessing
the fileExt variable.
A couple of thoughts:
- Is your
"json" really json? You didn't show us a sample,
so we don't know.
- Is your
"json" on a server, or just in a local filesystem?
Some browsers will not do Ajax with a local filesystem.
- Why go to the
server to fetch this file every time you need to do this? I
would just bake the data into the code. If you already have
the data as JSON, that's easy, because JSON is a valid
Javascript expression! Just copy the JSON string into your
Javascript file, and assign it to a variable!
- Or, you can just retrieve
the data ONCE and keep it around in a variable.
- Once you have
retrieved a JSON file from a server and parsed it, FORGET IT WAS
EVER JSON. Because once it is parsed, it is just an Javascript
object. There is nothing special or different about it vs. any
other Javascript object. JSON is just a way of encoding a
complex data structure into a string so that it can be
conveniently transported from A to B.