AJAX request fails .zip file OnceBuilder CMS - javascript

I'm building another full stack library but i'm so strugling on one AJAX request, which is so ridiculous. I have just found that, this function is strugling to get file archive but it can gets easly another files ( that can be solution but... i need zip file too) plugin 19 is static because it's just testing and only this file exists
#file_get_contents('https://oncebuilder.com/once/plugins/19/plugin.zip');
So basicly both AJAX function are the same
If i lunch the same function from preview mode file_get_contents gets crazy and return error, but if i lunch it from the listing or via browser its ok
AJAX function are both the same
$.ajax({
type: 'POST',
url: once.path+"/ajax.php?c=plugins&o=item_download&id=19",
contentType: "application/json",
dataType: 'json'
})
.error(function() { console.log("Request Error: item_download"); });

Related

Send request to download a zip file from server via Ajax in Laravel

I am using Laravel 5.7 and I want to download zip files on request basis. I send the request to the server via Ajax (the following code), and it returns 200 OK, but does not download the zip file (I can see the content of the zip file in the console though). Also, when I make a regular request (using form to submit the request) it downloads the zip file (as expected), but not with the Ajax request. The reason which I want to use Ajax is to add loading spinner during the downloading the files. Am I missing anything here?
$.ajax({
url: "my_url",
type: 'POST',
async: true,
beforeSend: function() {
$("#loading").show();
},
data: { selectedIds: ids },
success: function(result) {
//
},
complete:function(result) {
$("#loading").hide();
}
});

Build jquery url in $.ajax call

I'm trying to make a $.ajax call and send some data to a php file, my php file is located in a
component's folder and my js file in the webroot folder, how can I make the url portion of the $.ajax point to the right php file, to get to the php file from the JS file I need to do the following
../../../src/Controller/Component/AuthComponent.php
but when I do the same in the $.ajax call like
$.ajax({
type: 'POST',
url: '../../../src/Controller/Component/AuthComponent.php' ,
data: "accessToken" + access_token,
dataType: 'json',
complete: function (response) {
console.log("data coming back from Auth.php" +response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
I only get
http://localhost:8765/src/Controller/Component/AuthComponent.php- 404 not found
How can I build the url?
If you are using a framework or if you have build routing yourself, for all other pages of the website. Then similar to those, you will have to create (assign) a new route to the method() added in your AuthComponent.php for example:
Route: http://localhost:8765/authcomponent/method_name
Pointing to: /src/Controller/Component/AuthComponent.php::methodName()
Then, your final JavaScript code will look like:
$.ajax({
type: 'POST',
url: 'http://localhost:8765/authcomponent/method_name' ,
data: "accessToken" + access_token,
dataType: 'json',
complete: function (response) {
console.log("data coming back from methodName() of AuthComponent.php file" +response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
I would rather pass in the complete URL (if there's any chance you can pass on your domain to the JS code).
Sometimes, if you are compiling your code, the actual relative path might not be the same as your directory's ordering when running the code (e.g. the JS might be being compiled to run from a single location in a different folder within your site's dir).
you can't use absolute path as url ,if for example your document root is something called 'public_html' directory ,i mean your site name pointing to this folder and inside that your file AuthComponent.php is placed that your url is sitename/AuthComponent.php will make call to your file.

How to get data from local JSON using Ajax?

Hello brilliant people!
I've been sitting hours trying to figure out why I cant load a local JSON file using Ajax. I bet it's some silly typo, of that I totally misunderstand something. I'm using Node.js Express.
The location of the two files:
"trollytrains/views/trips.ejs" (Doing the ajax-request)
"trollytrains/json/allstops.json"
$.ajax(
{
type: "GET",
url: "../json/allstops.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('success');
},
error: function (msg) {
alert("error");
alert(msg.responseText);
}
});
I have tried altering the URL in all kinds of variations:
./allstops.json
allstops.json
json/allstops
/json/allstops
/json.allstops.json
No matter what I type I get a 404 error.
GET http://localhost:1337/json/allstops.json 404 (Not Found) jquery.min.js:4
Am I entering the wrong URL, or is there some other problem in my code that I haven't detected? I'm very new to JSON and Javascript so I wouln't be surprised.
I'd really appreciate some tips!
Best
/ J
Am I entering the wrong URL, or is there some other problem in my code that I haven't detected?
It depends on whether you can access the URL in the request in your example:
GET http://localhost:1337/json/allstops.json 404 (Not Found) jquery.min.js:4
See if you see the correct JSON when you go to http://localhost:1337/json/allstops.json with the browser. If you get 404 then this JSON is not served at this PATH and the problem can be either your backend failing to serve the JSON or you using the wrong URL.
It's impossible to tell you if you backend code is fine if you didn't include even a single line of the code that actually attempts to serve the JSON in question.
My assumption would be that you're not serving the JSON files.
I assume that you entire app is located in trollytrains directory and you have a json directory inside. To serve what's there you need to use something like this if you're using Express:
app.use('/json', express.static(path.join(__dirname, 'json')));
For more options to serve static files with and without Express, see this answer:
How to serve an image using nodejs
Try this.
$.ajax({
url: '../json/allstops.json',
dataType: 'json',
data:
async: false,
success:function()
{}
});

jQuery AJAX not receiving JSON from http request

I ame using html with some jQuery to try out some JSON requests. I did a bit of research and tried making something small just to test it out. but when i run the script in my browser(Google Chrome) i dont get anything besides my html/css stuff. here is the code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
console.log("Test");
console.log($.get("https://prod.api.pvp.net/api/lol/euw/v1.1/summoner/by-name/kerrie?api_key=[key]"));
</script>
*[key] is my key from the api owners(not to be shared on the internet).
when i check the network tab it says "304, not modified" i dont if this has anything to do wit it.
I'm just starting with websites and JavaScript/jQuery any help would be helpfull.
For better understanding you can call ajax method as below
$.ajax({
url: 'https://prod.api.pvp.net/api/lol/euw/v1.1/summoner/by-name/kerrie?api_key=[key]',
type: 'GET',
dataType: 'JSON',
async: false,
error: function(){},
success: function(resp){
// resp variable will be your JSON responce
}
}
});

parse xml using jQuery

i am parsing Xml using ajax function in jquery in a jsp file
$.ajax({
type: "GET",
url: "sites.xml",
dataType: "xml",
success: function(xml) {
}
});
My problem is my xml file is not in the same path as my jsp file. The jsp path is webProject/webcontent/temlates/store/SearchResult.jsp and the xml path is webProject/webcontent/WEB-INF/config/ampliflex.xml.
What should my url be in the $.ajax({ function?
Please Suggest
I would have thought that the WEB-INF directory would be private by default and so not accessible via an AJAX request. Try moving the XML file to some other part of your project.
Why not simply use:
$.ajax({
type: "GET",
url: "webProject/webcontent/WEB-INF/config/ampliflex.xml",
dataType: "xml",
success: function(xml) {
}
});
Do you see the file if you try accessing it directly with your browser (via localhost i mean)? if your ajax request can see the file so can your browser directly. i would suggest trying to find the correct url in your browser and use that in your ajax request.
Furthermore as mentionned above it is possible the WEB-INF directory is not accessible to client (hence my first question in this comment) so you can also try moving the xml file to a directory you know is accessible.

Categories

Resources