getting the list of files inside a folder using ajax - javascript

I have an url that is listing the files inside a folder:
Using javascript I need to obtain that list of files.
My code:
$.ajax({
url: 'http://webtests02/pruebas/',
success: function (data) {
console.log(data);
}
});
The thing is I am getting nothing as the return of the ajax GET call.
I tried other formats like using $.get but the result is the same so I cloncluded that or as it is not a .html, .aspx I can't get the code and scrape the content or the IIS is blocking these requests so I can't get it using jquery...
The website is mine so I can configure whatever needed in the IIS...
EDIT: I created an asp website that lists the files in the folder:
But I am still getting an empty response when I make the ajax call:
EDIT: THREAD CLOSED. It was due to a CORS authorization error.

Newer implementations of jQuery use the .done() method on the Ajax object to deal with completion and data of the AJAX call.
$.ajax({
url: "http://fiddle.jshell.net/favicon.png",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data.slice( 0, 100 ) );
}
});
http://api.jquery.com/jQuery.ajax/#jqXHR

Related

Why does making a python Requests request work and Jquery ajax Get does not?

I am trying to make a ajax call with Jquery to retrieve json data from a website. I can not get it to work in Jquery but I was able to make it work using the python Requests Library. How can I get the ajax data using jquery?
This code works and gets me the data I want (However, I want it client side not server side)
import requests
request = requests.get('http://volaris.com/locations/json/autocomplete/?term=tij&where=&loc=&lang=en')
This code does not. This is the code I would like to work. As of now, it only hits the fail function.
$.ajax({
url: 'http://volaris.com/locations/json/autocomplete/?term=tij&where=&loc=&lang=en',
type: 'GET',
daType: 'json',
cache: true,
error: function() {
alert( "ajax error" );
},
success: function(json) {
alert( "ajax success");
}
});

Feeding webpage data via remote Web API

I'm trying to operate two servers.
MVC Web API service.
MVC Web application.
The idea is that the web application renders a page filled with javascript requests, which populate the actual data from the remote API service. The web application will never itself touch the database, or the API service (besides setting up authorisation tokens initially).
What is the best way to achieve this?
So far I've been using JQuery AJAX requests, attempting to use JSONP. However I always get "x was not called" exceptions.
$.ajax({
url: '#(ViewBag.API)api/customer',
type: 'get',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: function (data) {
debugger;
// code to load to ko
},
error: function (xhr, status, error) {
alert(error.message);
}
});
Also the jsonpCallback function is called before the request is sent, so I assume its actually trying to call a function to generate a string? If I reform this request:
window.success = function (data) {
debugger;
// code to load to ko
};
... with jsonpCallback being "success", I still get the same error (but the success method is never called.
Any ideas? Thanks.
Edit: I've gotten started on the right course from this:
http://www.codeproject.com/Tips/631685/JSONP-in-ASP-NET-Web-API-Quick-Get-Started
Added the formatter, and replaced jsonCallback with success, like a normal ajax. However this only seems to work for get. I cannot delete or update. :(
Can you get it to work using the $.getJSON method?
$.getJSON( "ajax/test.json", function( data ) {
//handle response
});
Have you fired up developer tools in IE and watched the network traffic to see precisely what is being sent and returned?

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

jQuery $.getJSON not working

I am try to get a URL from a one server and using that URL to get contents of another server.
$.ajax({url : 'http://localhost:8080/geturl.jsp?A=1&B=2,C=3',
success : function (data)
{
alert(data);
$.getJSON(data, function (mydata)
{
alert(mydata);
});
},
error : function (data, status, xhr)
{
}
});
I know that we cannot make cross-domain requests in through ajax call, thats why i am using getJSON, i have the following problems
When i simply pass the data to the url part of getJSON (as shown in the code), the alert-box show the correct URL but no get request is being performed ( get requests were monitored from FireBug).
When a hard-code the data to be "http://www.google.com" then the get request is being performed but the no response comes, although the response headers comes and response code is 200 (but it was marked as RED in the Firebug (Dont know why :( )
When I tries to fetch a webpage host in localhost domain, then it is fetched correctly although the response was not JSON.
I have the following doubts
If the getJSON function accecpts only JSON objects as reponse then why no error came when perform above 3.
Whats the correct code to perform my the required functionality.
Suggestions to what happened in each case
Thanks in advance for the answers :)
The getJSON function can only be used across domains to fetch JSONP.
It does not magically evade any security restrictions.
http://api.jquery.com/jQuery.ajax/
This should be a working example for jsonp:
var request = jQuery.ajax(
{
url: "http://Your url",
success: function (data) { console.log('success!'); console.log(data); },
error: function (data) { console.log('error!'); console.log(data); },
dataType: "jsonp",
type: "GET",
data: { key: 'value' }
});

Referencing JSON response to POST of form data with jQuery and the Form plug-in

I'm having trouble understanding how to use jQuery and its Form plug-in to access the data returned by an HTTP post operation.
For example, I would like to POST data in a form from a browser, process the data on the server, return a server response (in JSON) to the browser, and display information about that response to the user in an alert -- without rewriting the form.
I thought the following jQuery code would do this:
$(document).ready(function() {
$('#ajaxFormSubmit').bind('click', function(event) {
$('#data_entry_form').ajaxSubmit({
url: "data_entry_ajax",
dataType: "json",
success: function(data) {
alert('Success');
alert(data.save_status);
}
});
});
});
The server returns the following in JSON object:
{'save_status': 'OK', 'id_number': 2}
But instead of displaying the two alerts over the form and user-entered data, the browser is wiping out the form and displaying the JSON reply.
I thought the "success" value in the options submitted to ajaxSubmit was automatically passed the response received from the server. I've tried various permutations of passing in arguments to the function body (referring to data without passing it in, referring to responseText with and without passing anything in), but these don't work either.
I've looked at "jQuery In Action" to try to understand accessing the response to an xhr object and a .ajax() function, but that hasn't helped either.
Try using jQuery $.post()
<script>
// ...
$('#ajaxFormSubmit').bind('click', function(event) {
$.post(
'data_entry_ajax',
$('#data_entry_form').serialize(),
function(data) {
alert('Success');
alert(data.save_status);
},
'json'
);
});
</script>

Categories

Resources