download a file from url - Javascript ajax - javascript

There's a CSV file uploaded to the server that I want to parse using javascript/jquery.
I'm trying to get the file using ajax call but it's always giving me error.:
XMLHttpRequest cannot load
https://1fichier.com/?w5hfqz60tk&_=1474818392318. No
'Access-Control-Allow-Origin' header is present on the requested
resource.
$.ajax({
url:'https://1fichier.com/?w5hfqz60tk',
type: "GET",
dataType: "text",
success: function (data){
parseFile(data);
},
error:function(e){
}
});
I need to run the above code in jsFiddle.. How can I bypass this error?
Or is there any alternative way to download a file?
Update: I just found out that adding url like this: https://crossorigin.me/MY_HTTP(S)_LINK solved my problem but I'm looking for an authentic way.

How can I bypass this error? Or is there any alternative way to
download a file?
You can use $.getJSON(), YQL
var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D'https%3A%2F%2F1fichier.com%2F%3Fw5hfqz60tk'%0A&format=json&callback="
$.getJSON(url, function(data) {
console.log(data.query.results)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Related

Get prayer time JSON data using JQUERY and Ajax

I want to READ JSON data using Jquery ana Ajax from this link
http://praytime.info/getprayertimes.php?lat=31.950001&lon=35.9333&gmt=180&m=3&y=2013&school=0&format=json&callback=?
and this is my code:
$(document).ready(function() {
var strUser ="http://praytime.info/getprayertimes.php?lat=31.950001&lon=35.9333&gmt=180&m=3&y=2013&school=0&format=json&callback=?";
$.ajax({
url: strUser ,
dataType: 'jsonp',
success: function(data){
jQuery.each(data, function(){
alert("yes");
});
}
});
});
I tried this code with other links , and it's correct, but from the specified link I don't get any out put, can you help me??
The url you are trying to access with JSONP doesnot support it. The server will need to return the response as JSON, but also wrap the response in the requested call back. So a way to solve this problem is using a server side proxy, which fetches the response from the specified url and passes it on to your client side js, like:
$.ajax({
type: "GET",
url: url_to_yourserverside_proxy,
dataType: "json",
success: function( data ) {
console.log(data);
}
});
where
url_to_yourserverside_proxy is a server side file that fetches response from the url specified
URL is outputting json but for cross domain need jsonp.
Not all API's provide jsonp. If cross domain API doesn't provide jsonp and isn't CORS enabled you will need to use a proxy to retrieve data due to same origin policy

Can't load json data in jquery

I am trying to get json data from twitter . http://api.twitter.com/1/statuses/public_timeline.json
this is my code
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("http://api.twitter.com/1/statuses/public_timeline.json",function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
</body>
</html>
it is from http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_getjson
but it is not working !
You are running in the same origin policy in JavaScript. This basically says, that you can't access resources from different domains (in your case the other domain would be twitter.com).
One solution is to use JSONP. The twitter API supports this. You can run a JSONP-request with jQuery in your case like this:
$.ajax({
url: "http://api.twitter.com/1/statuses/public_timeline.json",
cache: false,
dataType: 'jsonp',
success: function( result ){
$.each(result, function(i, field){
$("div").append(field + " ");
});
}
});
Besides, w3schools is no reliable source for information. Better use something like the Mozilla Developer Network.
Check out the json document you are trying to load. It returns the following error when I try to access it:
{"errors":[{"message":"Sorry, that page does not exist","code":34}]}
I would try replacing the url with a working api call to twitter before you worry about whether the code snippet is working. :)
Your trying to get a document from a different domain and the requested URL is blocking your request due to Cross-Origin Resource Sharing. You can read up on that here: http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
You can use JSONP with jQuery which should allow you to complete the request but that page you are requesting gets a 404 error so there's probably not much you can do with the result anyway.
You can Access json data from other domain using $.ajax() jquery method .See the code example below
$.ajax({
url : " // twitter api url",
dataType : "JSON",
cache : false,
success : function(success)
{
$.each(success, function(index,value) {$("div").append(value + ""); })
}
error : function() { //Statements for handling ajax errors }
});
The url you are using in that ajax request is returning a json array which contains an error
{"errors": [{"message": "The Twitter REST API v1 is no longer active. Please
migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.", "code": 68}]}
You need to migrate to API v1.1 before process the request , Replace the twitter api url with the new json url which is provided in API v1.1

Get a JSON file from URL, XMLHttpRequest cannot load error

I need to read a JSON file from URL and display.
I have read a lot of posts but still couldn't fix the problem.
url : http://webapp.armadealo.com/home.json
I face this error : XMLHttpRequest cannot load
The code is below
$.getJSON("http://webapp.armadealo.com/home.json", function(data){
alert(data);
});
I have tried adding to the url
&callback=?
and making it a jsonp, still no luck. I have also used
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
still no luck.
Is there anything that we need to do at the server side?
People who have faced such an error and have found a solution, Help me out please!
Thanks a lot!
You cannot make cross-domain AJAX requests like that due to security reasons. So if you want to load content from another domain, you will have to use a workaround: JSONP (more info, example)
Use the following code for the AJAX request:
$.ajax({
url: 'http://webapp.armadealo.com/home.json',
type: 'GET',
jsonpCallback: 'myCallback',
dataType: "jsonp",
success: function(data) {
console.log(data);
}
});
In order for this to work, you will have to wrap the JSON data in parentheses and add the callback name at the beginning:
myCallback({ ... JSON ... })
EDIT: Just noticed you already tried to use JSONP... Well, at least the above code works for me, perhaps you want to give it a try. ;)

How to parse XML from another server using JavaScript or jQuery?

I am new to Javascript. I need to parse XML using javascript or jQuery which is another server. I tried by using the below code. But when I executed it, it was not going to success method.
I was able to parse the XML which is in the same folder. Is it possible in javascript to access content from another server. I read the Same Origin Policy.
I was able to get the success message, but cant get the xml data
$.ajax({
type: 'GET',
url: 'http://10.47.5.69/myxml.xml',
dataType: "xml",
success: function(data){
alert('success');
$(data).find("Node").each(function() {
alert($(this).find("element").text());
});
},
error: err
});
function err(xhr, reason, ex)
{
alert('xhr.status: ' + xhr.status);
alert('ex "'+ex);
}
You cannot load something from another server due to cross-domain security checks.
However for javascript, there is a workaround: the JSONP technique: http://en.wikipedia.org/wiki/JSONP
It is used for JSON data but it can just as well be used for any string data. But it will only work if you have some degree of control (i.e. can install a script) on that server.
Another alternative is to proxy that URL on your own server. That might be easier.

Ajax get request to wsdl web-service

i have wsdl file and i need get data from. How i can do this ? i'm trying do this with ajax
like this:
jq.ajax({
url: 'http://url.wsdl',
type: 'get',
success: function(data){
alert("OK " + data);
},
error: function (x, y, z) {
alert("ERROR");
}
});
what i do wrong ?
Any other way to get data from wsdl web service using javascript, jquery and etc. is?
I think what you are missing is a data: {}
I read that there was some sort of bug if you did not include that when using $.ajax
Oh, and most likely you are going to need dataType: "json" or whatever datatype the service is using.
Here is an example i am using against an online webservice:
jQuery.support.cors = true; //enables cross domain queries for Ajax
$('#jqueryBtn').click
(function ()
{
$.ajax
(
{
type: "GET",
url: "http://www.webservicemart.com//uszip.asmx/ValidateZip",
data: { 'ZipCode': '22553' },
dataType: 'html',
success: jqSuccess,
error: jqError
}
);
}
Hopefully you can use this example to fix your own code
http://forum.jquery.com/topic/jquery-ajax-to-call-a-wsdl-web-service
The following link should explain why you cannot use AJAX for cross domain queries:
http://www.w3schools.com/xmL/xml_parser.asp:
Access Across Domains
For security reasons, modern browsers do not allow access across
domains.
This means, that both the web page and the file it tries to load,
must be located on the same server.
The examples on W3Schools all open XML files located on the W3Schools
domain.
If you want to use the example above on one of your web pages, the file you load must be located on your own server.
You can create a proxy web page in your web server to access to WSDL web service and return result to the AJAX request

Categories

Resources