简体   繁体   中英

Is it possible to make an AJAX call to JavaScript instead of ASP?

I am making somewhat of a content delivery network (CDN), but I want to have a jQuery AJAX call to a JavaScript file instead of an ASP file. The JavaScript file would then handle the input and then return a response. I don't want to use ASP, because I don't want to set up a whole server, especially because I am on a Mac. If this isn't possible that's fine, I'm just wondering.

(I can not provide any code, because I don't have enough that it would be useful.)

If you want to use AJAX to retrieve a JavaScript file, that's kinda possible. You'll just need to dynamically add a <script> tag to the page (with the URL of the JS file in question) and the browser will take care of the rest. Don't forget you can add an onload handler to execute some code when the file's loaded.

If, however, you wanted to run JavaScript on your server, there's the excellent NodeJS for that. You will need to have another server running in this case.

Either way, there's not really a way to have code running outside of the browser and not have a separate server. Hope this helps, comment with any further questions.

I wonder if you're just trying to lazy load a JavaScript file from a URL, but that isn't your question, so I'll ignore that.

You can do just about anything, but that doesn't mean you should. You can't directly make an Ajax call to JavaScript. That's just a meaningless statement... The semantics of what you actually said would require an Ajax call to a server that has the ability to execute JavaScript code like a browser would, which can be done with a headless browser like PhantomJS . So, you would have to install PhantomJS on your server, Ajax to the server, have the server use PhantomJS to process some JavaScript code, then have the server respond to the Ajax call with whatever result you come up with.

I have to reiterate: You should not do this ^.

It would be like traveling the world to get to your neighbor's house. Anything that is possible within all of that mess, you could have just done it in the JavaScript code on the page or on the server in the server-side language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM