I'm trying to access a JSON api of a certain adult-oriented website, but I can't get it to work. Here's the jsfiddle: http://jsfiddle.net/SSqwd/ and here is the code:
$.ajax({url: 'http://api.redtube.com/?data=redtube.Videos.searchVideos&search=sex&thumbsize=big&page=1&output=json&callback=?',
dataType: 'json',
success: function(data) {
console.log(data);
}
});
I get
Uncaught SyntaxError: Unexpected token :
even though when I browse to the page myself and check the json on jsonlint, it is perfectly valid.
What's going wrong here?
Since you specified a cross domain request jQuery will by default attempt to request that URL using JSONP even though you've only set the dataType to JSON. So what actually happens is the following script tag is added to the DOM:
<script src="http://api.redtube.com/?data=redtube.Videos.searchVideos&search=sex&thumbsize=big&page=1&output=json&callback=?" type="text/javascript"></script>
(the ? actually gets replaced with a function name jQuery generates)
Your browser then tries to execute the returned data. However, as it is not actually JSONP and is just pure JSON what it returns is invalid javascript and hence you get the Syntax error.
You can reproduce the same error by just using:
<script src="http://api.redtube.com/?data=redtube.Videos.searchVideos&search=sex&thumbsize=big&page=1&output=json&callback=?" type="text/javascript"></script>
And doing nothing else. Or you can copy and paste the response from that URL and wrap it in script tags, you'll get the same error thrown.
The only way to solve this error is use an API that supports JSONP or implement a sort of proxy that requests the data via your own server on the same domain.
Related
Ok, so the following is what I want to achieve:
I want to use jquery to load a file inside my javascript file.
Normaly you can do a $.ajax request with a POST to a .php file. And select something in a database that looks like the POST I sended with. Now I want the same thing, but instead using a .php file, I will use a .js file.
This also works, the code loads the file with the text alert('Hello World!'); and shows the alert. All fine. But what I want when I load that .js file, it needs to send a value with it. So it could be alert(data); and it will say hello world. How can i achieve this without using cookies or something else that stores values.
EDIT: this a GET request, a POST request is not allowed. Unless you're using GET variables to rewrite your javascript file before returning it you can't modify it or make it do anything other than what would happen if you just requested it with the <script> tag with the async attribute. /edit
Well I just learned something new. You can get a javascript file with an ajax call and execute it using eval but I'm almost 100% certain this is not how you should be doing whatever you're trying to do (also - when is JavaScript's eval() not evil)
$.ajax({
url: '/path/to/script',
type: 'GET',
success: function (response) {
eval(response.toString());
};
});
You're calling the server, which then returns the javascript file and you're parsing it as javascript using eval. I never considered doing this but I supposed it's one way to load a javascript file on demand.
I am trying to access a simple data file via javascript and finding it incredibly difficult. It will be placed on a remote server and hopefully could be accessed via http. I am new to javascript, but here goes: the best option so far I have found in JSONP, as stated here https://learn.jquery.com/ajax/working-with-jsonp/.
I have created an example datafile that I would have to process: http://murded.keeleleek.ee/test2.txt.
<html>
<head>
<title>My experiment</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
// Using YQL and JSONP
$.ajax({
url: "http://murded.keeleleek.ee/test2.txt",
// The name of the callback parameter, as specified by the YQL service
//jsonp: "callback",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
// Tell YQL what we want and that we want JSON
data: {
q: "dataprev",
format: "json"
},
// Work with the response
success: function(response) {
console.log(response); // server response
}
});
</script>
</html>
Eventually I would just like to use the JSON values as an array, and for now, just print them out to console or screen to make sure they are there. Is anyone able to help me troubleshoot this? Perhaps there is a much easier way?
Unfortunately I am new to both javascript and JSON, so there may be an easy newbie error there. The file should be on a remote server, and just read access should be enough for import. Many thanks!
A simple option is using $.getJson
$.getJSON(
"http://murded.keeleleek.ee/test2.txt",
data: {
q: "dataprev",
format: "json"
},
function( response ) {
console.log( response ); // server response
}
)
If your code, you said:
// Tell jQuery we're expecting JSONP
But your URL does not contain JSONP. It contains plain text.
In order to process it as JSONP you must format it as JSONP.
That means:
A core of JSON (See http://json.org/ for examples)
Wrapped with a JavaScript function call: foo(YOUR_JSON);
Served with an application/javascript content type (so you should use a .js file extension if you are serving up a static file)
If you are using a static file, then you are forced to hard code the callback name (foo in the example above). You need to tell jQuery what that callback name is instead of letting it generate one dynamically:
jsonp: "foo"
NB: JSONP is a hack to work around cross domain issues. It imposes quite a few limits compared to using XMLHttpRequest, including restrictions on what you can do to handle errors. There is now good support for CORS in browsers, so JSONP isn't needed any more. If you aren't making requests across origins then you don't event need that.
I'm trying to use jquery to load a local html page and display an alterated version of it.
I think I can do the alteration just fine, but I can't seem to load the page because it contains malformed tags such as unclosed IMG or BR tags, or </ characters,
which is normally ignored by web browsers
but when I'm using the $.get("mypage.html"); command,
firefox's javascript console shows an error telling me about the malformed tags and in the end, the ajax response only contains the page which send the ajax request (that's normal considering the loading failed)
I'm guessing the solution would be to load the html page as plain text so firefox won't catch a malformed tag error but I can't manage to force the response type and so, I always get the same error.
so far, I tried to set the ajax plugin like so :
$.ajax({
contentType: "text/plain; charset=utf-8",
dataType : "text",
success: function(data) {
alert(data);
}
});
contentType and dataType settings give me a responseText
but the load still fails so it contains the source code of the calling page
is there really no solution to this problem ? besides the obvious "manually correct the tags in the html pages"
I have to add that all of this is local, there won't be any server processing involved.
I'm using the latest firefox (20) and the jquery (1.9.1) updates to this day
XMLHttpRequest triggers an HTTP request; therefore, I'm pretty sure that you can't avoid an HTTP server being involved.
I'm loading a large number of items on a page through JSON, but it isn't working. This is my first time using JSON and I figured JSON was just a big object so I copied my object that I had before in a variable into a file and names it fun.js.
You can check out the JSON here:
http://justpaste.it/15zc
I'm using jQuery to get the JSON:
$.getJSON('fun.js', function(data){
alert(data)
});
Nothing is being alert, in the matter of fact...the alert isn't happening at all. Anyone know why?
For starters, your JSON doesnt validate. Go paste it here and fix your errors: http://jsonformatter.curiousconcept.com/
Secondly, user jQuery.Ajax to which you can pass onError parameter so you'll get a warning that JSON didn't go through.
Have you validated that the JSON is the issue? Open up Firebug or Chrome Dev Tools and refresh the page. You may see a message that the file wasn't found or there was a parsing exception or a security error.
If the file isn't found, you can fix that in the code.
If there is a parsing error, use a JSON validator.
If there is a security issue, see my answer to Get a JSON file from URL and display. You need to update your server policy or configure your browser to allow access to file URL's in your tests through.
I'm trying to get a greasemonkey's script to work jquery and json.
this is the json url
http://www.sora101.net/auction.php?id=1&callback=
this is part of the script
$.getJSON("http://sora101.net/auction.php?id=1&callback=?",
function(data){
alert(data.id);
}
);
i always get something like this "Error: jsonp1282646809490 is not defined" in the console.
i also found this h**p://www.xucia.com/CrossSafe/test.html
on this site i get the right object returned but when i include this in my script it doesn't work...
can anyone help me? thanks and sorry for bad english
It seems like you should be using JSONP and now JSON as you're using different domains.
From jQyuery.getJSON() documentation:
Blockquote
JSONP If the URL includes the string
"callback=?" in the URL, the request
is treated as JSONP instead. See the
discussion of the jsonp data type in
$.ajax() for more details.