在AJAX调用node.js期间出错

时间:2016-11-11 12:10:01

标签: jquery ajax node.js

我正在尝试在Node.js中做一些示例,我在下面的URL中创建了对node.js的AJAX调用。

https://github.com/InteractiveIntelligence/CallbackSite/commit/39c6e5d82ae4541a2dd0be8f41fb1bacefe68ed6

我的节点服务在'localhost:8880'中运行,我的jQuery / AJAX调用在'localhost:2222'中运行。我使用此代码来调用节点(localhost:8880)。

<html>
    <head>
        <title>jsonp test</title>
        <!--<script src="JQuery.js"></script>-->        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {


                $('#select_link').click(function(e){
                   // e.preventDefault();
                    console.log('select_link clicked');

                    var data = {};
                    data.title = "title";
                    data.message = "message";

                    $.ajax({
                        url: 'localhost:8880',
                        type:'POST',
                        dataType: 'json',
                        crossDomain: true,
                        data: JSON.stringify(data),                     
                        contentType: 'application/json',
                        success: function(data1) {
                           console.log('success');
                            //console.log(JSON.stringify(data));
                        },
                        error: function (err) {

                            console.log(err.statusText);
                        }
                    });
                e.preventDefault();
                });

            });
        </script>
    </head>
    <body>
        <div id="select_div"><a href="#" id="select_link">Test</a></div>    
    </body>
</html>

我在我的本地IIS中托管了上面的页面,它在2222端口运行。在chrome浏览器上运行代码之后,我收到了以下错误。

  

XMLHttpRequest无法加载localhost:8880。交叉原始请求是   仅支持协议方案:http,data,chrome,   chrome-extension,https,chrome-extension-resource

当我使用firefox浏览器运行时,下面的错误就会丢失......

  

URI方案对应于未知的协议处理程序“nsresult:   “0x804b0012(NS_ERROR_UNKNOWN_PROTOCOL)”位置:“JS frame ::   https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js ::   .send :: line 4“data:no]

我的Index.js:

> var express = require('express') var request = require('request') var
> app = express();
> 
> //app.set('port', (process.env.PORT || 5000)) app.set('port',
> (process.env.PORT || 8880)) app.use(express.static(__dirname + '/'))
> 
> app.listen(app.get('port'), function() {   console.log("Node app is
> running at localhost:" + app.get('port')) })
> 
> 
> app.use(function (req, res, next) {
> 
>     res.header("Access-Control-Allow-Origin", "localhost:2222"); // restrict it to the required domain
>     res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
>     // Set custom headers for CORS
>     res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Custom-Header");
>     var pattern = /\/webtools\/(.*)\/(websvcs\/.*)/;
> 
>         if(req.url.match(pattern)){
>             host = 'http://' + req.url.match(pattern)[1] + ':8114'
>             var db_path = req.url.match(pattern)[2]
>             , db_url = [host, db_path].join('/');
> 
>             console.log('forward to ' + db_url)
> 
>             req.pipe(request[req.method.toLowerCase()](db_url)).pipe(res);
>         }else{
>             console.log('does not match')
>             next();
>         }
>     }
> 
> );

0 个答案:

没有答案