Json callback problem - javascript

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.

Related

Opening a remote file with JSON data in javascript

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.

How to get data in JSON from a API with jQuery? Without errors, like: cross domains, transferred with MIME type

Here dont working =(
A searching here of hours and hours and nothing. =(
the url API is: http://www.poatransporte.com.br/php/facades/process.php?a=nc&p=2821&t=o
It' return a json.
I try jsonp with jquery ajax and getJSON, but nothing. Somewhere the error is the data returned in MIME type, others the limited CORS. The interesting is: the response its ok (Google tools), but the console.log shows the errors mentioned above.
Some ninja could take the data from above url with jquery, and make some data appear in some div or give a alert()?
Now working!
With jQuery dont work because security etc... But in server side work fine. In PHP I use this:
<?php
$data = json_decode(file_get_contents('http://www.poatransporte.com.br/php/facades/process.php?a=nc&p=2821&t=o'));
print_r($data);
?>

Can't access another domain's valid json with jquery and ajax

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.

JQuery. text() does not seem to work in IE8

I have the following XML as a string:
<battery_content>
<last_update>2012-15-09-22-40</last_update>
<total_downloads>234</total_downloads>
......
</battery_content>
I get the XML from an Ajax request and I store it in sXMLData. I do a quick window.alert(sXMLData) and everything's fine.
When I run the next code in IE8, it won't seem to work. Chrome and Firefox work.
window.alert("last_update" + $(sXMLData).find("last_update").text());
I can't seem to figure out why. Does this method not work with IE8? If so, how can I solve the problem?
The proper way to handle "XML as a dumb string" is to pass it through $.parseXML first:
window.alert(
"last_update" + $($.parseXML(sXMLData)).find("last_update").text());
However, you wouldn't need to do this manually if
either the server returns an XML Content-Type,
or the AJAX request you fetch the XML with uses the dataType AJAX option to specify that the response should be treated as XML
If the server is under your control, fix it to return the proper content type. If not, use the alternative solution. I recommend parsing the XML manually only if you are getting the string from third-party code that you have good reason to not want to touch.

In JavaScript, is there a way to get source code of a given URL?

In JavaScript, is there a way to get source code of a page given its URL? Kind of equivalent to PHP's file_get_contents()
You can just download from a URL through XMLHttpRequest (or jQuery's ajax):
$.get(
'yourfile.js',
function(data) {
// The source code is in data
}
);
How about this: http://phpjs.org/functions/file_get_contents:400 ?
I can do this with Pretty Diff tool api: http://prettydiff.com/api.php
The actual application, prettydiff.js, is JavaScript, and the service processing the requests on the server is also JavaScript. To see this in action go to the test link and use the "Source URI" method of input.

Categories

Resources