I've got an url here
http://94.23.34.69:7240/
wich contains XML information. I need this information between the tags
<Name>IG_Battlegrounds</Name>
<MapName>Brezaliad</MapName>
<NumberOfActivePlayers>35</NumberOfActivePlayers>
<MaxNumberOfPlayers>50</MaxNumberOfPlayers>
When getting the information i need to print it into html.
I managed to get this trough PHP but that is taking to long and makes my website load slowly. I haven't worked with ajax yet. I hope someone can help me. I tried to use this
$.ajax({
type: "GET",
url: "http://94.23.34.69:7240/ ",
dataType: "xml",
success: function(xml) {
alert("xml");
}
});
But this is not working. It doesn't show anything. Do i need to include something special for using ajax? I also need al the information comming from this link and break up the info so i can use parts of it to print to my website
http://module.game-monitor.com/85.236.100.184:8876/website.php?s=test.css&f=18
Help would be appriciated.
You cannot send an Ajax request to another domain other than yours.
https://en.wikipedia.org/wiki/Same-origin_policy
You can circumvent it using CORS (if you can manage the server) or using a proxy.
Ways to circumvent the same-origin policy
Related
I have build a webpage that focuses a lot on an XML file to populate it. Now I generated a second one for some additional tasks. Unfortunatly, everytime I try to parse it, it dows not succeed.
I am using very simple code to demonstrate the problem. I stripped the function, so everything it does is writing strings into the browser console.
Once I excecute this code, all I get is "pos1, pos3, pos4"
So the success function is not even excecuted. I know this sounds like a dumb question, but it is really hard for me, to find my mistake right now.
function insertFirstBeginnerQuestion(){
console.log('pos1');
$.ajax({
url: 'xml/questions.xml',
type: 'get',
dataType: 'xml',
success: function(data) {console.log('pos2');},
error: function(){console.log('pos3');},
});
console.log('pos4');
};
Are you trying it in server? Your code works for me and loads the xml properly. GET is a HTTP method, so you have to deploy this in a server and access using http protocol. Otherwise this will give you 404 error while trying to load the xml file.
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 new to jquery and javascript and I've been trying to access the contents of a json file from an external link with no luck. The json file is produced in the link below.
http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x
I noticed that several examples have a url similar to this "www.samplesite.com/testfile.json" However, as you can see above, the URL is not like this. Opening the link in Chrome takes you directly to the json file contents, however opening the file in IE asks you if you want to save the file "A10,A11.json".
All I want to do is be able to display the json file content in HTML. Can some please show me a brief example.
Thank you
$.ajax({
url: 'http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x',
dataType: 'jsonp',
success: function(data){// your code here
}
});
You can access the JSON only if the website having the JSON allows cross origin resource sharing(CORS). Find out if that is available, and if it is then post your code.
$.getJSON('http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x', function(data) {
//data is the JSON string
});
To retrieve JSON from servers outside your own domain, you should set up the callback to retrieve so called 'padded' JSON, which is easily done by adding the following to the jQuery .ajax() function:
dataType: 'jsonp'
I want to parse json data from url which contain json data, but its extension is not .json
When I try to request response url using below code it make ajax request with 200 status code.
But I try with other url containing same data with .json extension, it works.
Here is my javascript code
var url='http://jsonurl.com/jsondata';
jQuery.ajax({
url: url,
dataType: 'json',
success: function(d){
jQuery.each(d,function(i){
console.log(i);
});
}
});
So I want solution to parse json content of file in javascript or in jquery, which dont have .json extension but contain json data .
CORRECTION: now seen your example url above and the content type is correct so that was a bit of a red herring.
The extension does not matter.
But the URL you are calling will need to return the correct content type which would be application/json Having said that our app uses text/javascript which works as I believe the browsers are a bit lenient.
So in my case I call a JSP getJSON.jsp that sets it's content type like this
<%# page language="java" contentType="text/javascript; charset=UTF-8" pageEncoding="UTF-8"%>
I am guessing that when you use the .json extention the server is returning with the correct type but your other extensions will not.
I found the exact problem, after discussion with support team of API.
Actually the problem is with content type. They implemented some cache system for that json data, and when my script make request to API url, it send that json data and creates its cache and that cache remain active for some time. If my script create same request, then that json data served from cache who's content type is not json.
Now they are working on this issue.
BTW thanks Peter for help. :)
Is it possible to have a RSS feed by using a Javascript code?
It is totally possible.
If you want to use a local feed (on your own domain), you can simply use AJAX and parse that RSS feed.. This does not, however, work cross-site (as you might know), and unfortunately you cannot use this to get RSS feeds from other domains. (If you don't know AJAX, you can learn it at W3Schools or Tizag).
But Google has a solution. Using the Google AJAX Feed API, you can read RSS feeds from other domains without AJAX. You can read the docs for it to get a basic understanding of how it works.
An RSS feed is a type of xml file at a url. I'm not sure how you'd accomplish that with javascript (unless it's server side javascript). I think you need to do some homework on what you are trying to accomplish.
It's entirely possible. Create an Ajax request with JQuery.
$.ajax({
type: "GET",
url: "[your xml rss url]",
dataType: "xml",
success: function(x) {
$(x).find('foo').children().each(function(){
if(this.nodeName == "bar")
{
var x = $(this).attr("bar1");
var y = $(this).attr("bar2");
var z = $(this).attr("bar3");
return;
}
});
Jquery-Ajax
That depends what you mean. Probably not.
look at this jQuery plugin. It works great
http://www.zazar.net/developers/jquery/zrssfeed/