How to process TFL news feeds? - javascript

Can anyone suggest an idea of accessing tfl traffic news feeds?. The following is the link to get the url for the data feed.
http://www.tfl.gov.uk/businessandpartners/syndication/16492.aspx
The code I tried is as follows:
Code Sample:
$jq.ajax({
type: 'GET',
url: 'http://www.tfl.gov.uk/tfl/businessandpartners/syndication/feed.aspx?email=***********#****.com&feedId=13',
dataType: "xml",
async: "false",
success: function(xml) {
$jq(xml).find('rr_event').each(function() {
//var title=$jq(this).find('title').text();
alert("success!");
});
}
});​

It looks like the link requires you to have valid credentials to access what I expect to be some sort of xml feed? Also, if you are trying to access this url from a different domain other than www.tfl.gov.uk, you will need to use JSONP to fetch the data otherwise you will run into a security exception (cross-domain error).

Related

jQuery syntax error for a valid json

I am trying to get the instagram user details using jQuery ajax which can be found from the url
https://www.instagram.com/instagram/?__a=1
But when i call the same through ajax from my following code
$(document).ready(function(){
var instagram = 'instagram';
$.ajax({
url : 'https://www.instagram.com/'+ instagram +'/?__a=1',
dataType: "jsonp",
success: function(e) {
document.write(JSON.stringify(e));
}
});
});
It is returning me a error :
missing ; before statement [In Json]
However when i copy and paste it for validation on some online tools available it is showing valid formatted json.
DEMO : https://codepen.io/abhibagul/pen/WXGoYb?editors=0010
Error is visible in the browser console.
If you are getting respone as JSON,
Change from dataType: "jsonp" to dataType: "json"
Valide json response would look like
{
"Name": "Foo",
"Id": 192.168.73.96,
"Rank": 7
}
And jsonp would be like
parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
Get more details form here
Instagram does not support jsonp, it is not used much anymore in favor of CORS. Change jsonp to json. However, this will not work because Instagram's CORS policy prevents it. You will need to use the API provided by Instagram.
$(document).ready(function() {
var instagram = 'instagram';
$.ajax({
url: 'https://www.instagram.com/' + instagram + '/?__a=1',
dataType: "json",
success: function(e) {
document.write(JSON.stringify(e));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Cross domain ajax causing issues to render data

I trying to capture data from an HTML form which will be placed on another website. From that form I need to capture data into my website. But when I tried jQuery Ajax call for cross domain it shows me 302 error with no response.
I've tried this
$('button[type="button"]').on('click', function(){
var data = $('.data-capture-form').serialize();
$.ajax({
type : 'POST',
url: 'http://prospectbank.co.uk/leads/test',
dataType: 'jsonp',
crossDomain : true,
data : data,
contentType: 'application/jsonp'
}).done(function(res){
var resp = $.parseJSON(res);
console.log(resp);
});
});
Where is problem with this code? Any help?
Fiddle Code
If you have access to the server, add the following header:
Access-Control-Allow-Origin: *
And then make JSONP calls
$.ajax({
type: "POST",
dataType: 'jsonp',
...... etc ....
If you do not have access to the external server, you have to direct the request to your server and then make a proxied call to the external server.

Uncaught SyntaxError: Unexpected token : ajax call

So I'm trying to query the below json feed, however I keep getting the error in the topic.
I've searched around this site for possible answers however none that I've come across have worked so far. Commented out datatype and jsonp, jsonpCallback isnt it either, either is data, I've made sure that it validats via http://jsonformatter.curiousconcept.com/ and it does. I really dont know.
$.ajax({
type: 'GET',
url: 'http://raidbots.com/json/playerdata/us/mannoroth/usiris',
cache:true,
dataType: 'jsonp',
data: {
format: 'json',
},
success: ranks,
jsonpCallback:'callbackName',
error: function(data) { console.log(data); },
jsonp: false,
});
function callbackName(data){
console.log("jsonpCallback");
}
var ranks = function(data) {
console.log(data);
}
Thank you
-Art
The error is in your JSONp data because it's just JSON and not JSONp. JSONp requires the document to be valid JavaScript containing a function call.
If they don't support jsonp you need to use a proxy script (e.g. a php script on your server that retrieves the document) or ask them to send CORS headers so you can use a normal non-JSONp AJAX call to retrieve the data directly.

Consuming web service in HTML

I have created a web service and saved its .wsdl file. I want to consume this service in my HTML file by providing its URL.
Can anybody tell me how to call the URL in HTML and use it?
Like its done here.
http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr/
The only difference is my web service does not have an extention like "asmx?wsdl".
Does that makes any difference.
I followed that tutorial too but it does not produce any output.
Thank You////
You definitly should get familiar with AJAX.
You could use the ajax functionalities provided by jQuery. That's the easiest way, I assume. Take a look at http://api.jquery.com/jQuery.ajax/
You can use this like
$.ajax({
url: "urltoyourservice.xml"
dataType: "xml",
}).done(function(data) {
console.log(data);
});
HTML itself cannot consume a webservice. Javascript ist definitely needed.
The existence of your WSDL File looks like you are probably using a XML format as the return of the web service. You have to deal with XML in javascript. Therefore take a look at Tim Downs explanation.
But keep in mind, that your web service URL must be available under the same domain as your consumption HTML-File. Otherwise you'll receive a cross-site-scripting error.
yes you can use ajax but keep in mind you wont be able to make a request across domains. Consuming web services should be done in server side.
To further gain more knowledge about this, read How do I call a web service from javascript
function submit_form(){
var username=$("#username").val();
var password=$("#password").val();
var data = { loginName: username, password:password };
$.ajax( {
type: "POST",
url:"Paste ur service address here",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
var value = data.d;
if(value.UserID != 0){
alert.html('success');
}
},
error: function (e) {
}
});
}
Try this it will help

How to send XML request in JavaScript

I have an eye tracker (mirametrix) running as a server in localhost. In order to capture data from this I should send a xml request as given below;
SEND:<GET ID="TIME_TICK_FREQUENCY" />
I have a web-app running as the client and I need to make a request to this server through that. I wrote the following code but it doesn't seem to send the request properly to the server.
var request = $.ajax({
url: "http://127.0.0.1:4242",
type: "GET",
dataType: "xml",
data: {
data:'<GET ID="TIME_TICK_FREQUENCY" />'
},
});
Can someone please help me with this ?
Thanks in advance
$.ajax({
url: ajaxurl,
data: "<root><blah><blah></root>",
type: 'POST',
contentType: "text/xml",
dataType: "text",
success : parse,
error : function (xhr, ajaxOptions, thrownError){
console.log(xhr.status);
console.log(thrownError);
}
});
I think you need to post it.
this is not plain javascript.. looks more like jquery.. also what do you mean with 'properly'. It's kind sending it, but not really? Aside from that, I see the following problems
The url is wrong, and although your browser may handle it, your url should end with a slash.
Your xml is invalid. Needs to start with <?xml version="1.0"?>.
If you accessed this script through a different host than 127.0.0.1., or if the script is running on a different port than 4242, this will not work without CORS headers on the server.

Categories

Resources