简体   繁体   English

使用jQuery从PhoneGap应用向Node JS服务器发出AJAX请求

[英]Making an AJAX request with jQuery from PhoneGap app to Node JS server

I've been looking around on the internet to see a real example of a jQuery AJAX request to a Node JS server, but can't find it. 我一直在互联网上四处查看,以看到向Node JS服务器发送jQuery AJAX请求的真实示例,但找不到它。

So normally a jQuery AJAX request to a PHP server looks like this: 因此,通常对PHP服务器的jQuery AJAX请求如下所示:

$("button").click(function() {
    $.ajax({url: "http://www.example.org/myphpfile.php", success: function(result) {
        console.log(result);
    }});
});

But how does this JS code look when a request must be made to a Node JS server? 但是,当必须向Node JS服务器发出请求时,此JS代码的外观如何? I've not seen any example that has a URL to a .js file on the server, but we do include a URL to a PHP file when a request is made to a PHP server. 我还没有看到任何在服务器上具有指向.js文件的URL的示例,但是当向PHP服务器发出请求时,我们的确包含了指向PHP文件的URL。

So in short, what does a jQuery AJAX request to a Node JS server look like? 简而言之,对Node JS服务器的jQuery AJAX请求是什么样的? Or are there any additional libraries or frameworks needed to achieve this? 还是需要其他库或框架来实现这一目标?

NodeJS dosent work like that. NodeJS的工作原理就是这样。

You set up and start the server on a port let say http://localhost:3000 and you send all request to it. 您在端口http:// localhost:3000上设置并启动服务器,然后将所有请求发送给它。

You can use a web application framework like Express to define routes more like .htaccess in php does. 您可以使用像Express这样的Web应用程序框架来定义路由,就像php中的.htaccess一样。 You setup functions on those routes. 您可以在这些路线上设置功能。

So a typical call to a NodeJS looks like 因此,对NodeJS的典型调用看起来像

http://localhost:3000/route1

route1 isn't a file its a route configured by a web application framework like Express to perform a task (or function) when the route is reached route1不是文件,它是由诸如Express的Web应用程序框架配置的路由,以在到达路由时执行任务(或功能)

So jQuery has nothing to do with it. 因此,jQuery与它无关。 You can still use it has it was just let url point to the route defined by Express 如果只是让url指向Express定义的路由,您仍然可以使用它

$("button").click(function() {
    $.ajax({url: "http://www.example.org/getWhatever", success: function(result) {
    console.log(result);
  }});
});

This tutorial will help https://scotch.io/tutorials/use-expressjs-to-get-url-and-post-parameters 本教程将帮助https://scotch.io/tutorials/use-expressjs-to-get-url-and-post-parameters

This Book is great www.packtpub.com/web-development/mean-web-development 这本书很棒www.packtpub.com/web-development/mean-web-development

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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