Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jqueryajax请求没有';我无法到达服务器_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript jqueryajax请求没有';我无法到达服务器

Javascript jqueryajax请求没有';我无法到达服务器,javascript,jquery,ajax,Javascript,Jquery,Ajax,我编写了以下函数,用于从浏览器发送AJAX POST请求: function addFormToDB(email, company, subject, text) { var params = "email=" + email + "&company=" + company + "&subject=" + subject + "&text=" + text; $.ajax({ url: 'http://127.0.0.1/submit',

我编写了以下函数,用于从浏览器发送AJAX POST请求:

function addFormToDB(email, company, subject, text) { 
   var params = "email=" + email + "&company=" + company + "&subject=" + subject + "&text=" + text;
   $.ajax({
      url: 'http://127.0.0.1/submit',
      type: 'POST',
      data: '{"data":"' + params + '"}' ,
      xhrFields: {
         withCredentials: false
      },
      dataType: "jsonp",
      contentType: 'text/plain',
      success: function(data) {
         alert("success");
      },
      error: function(result) {
         alert("error");
      }
   });

}
在服务器端(node.js+express),我有以下处理POST请求的功能:

app.post('/submit', function(req, res) {
    console.log("enter function");
    var p = new Promise(function(resolve, reject) {
        db.serialize(function() {
            db.run("INSERT INTO users VALUES (?, ?, ?, ?)",
            [req.query['email'], req.query['company'], req.query['subject'], req.query['text']],
            function (err) {
                if (err) {
                    console.error(err);
                    reject();
                } else {
                    console.log("Transaction passed");
                    resolve();
                }
            });
        });
    });
    p.then(function(){
        res.status(200).send();
    }).catch(function() {
        res.status(400).send();
    })
});
我不知道为什么,但是当POST请求被发送时,什么也没有发生,程序也没有进入POST请求的功能。控制台什么也没说

这是“网络”窗口的外观:

我理解404错误代码意味着路由有问题。但是,当客户机代码为this(无JQuery)时,它可以正常工作:

var params = "email=" + email + "&company=" + company + "&subject=" + subject + "&text=" + text;
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://127.0.0.1:3000/submit?" + params, true);
    xhttp.onreadystatechange = function() {
        console.log(xhttp.readyState + " " + xhttp.status);
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            console.log("request " + params + " was sent to DB");
            alert("Thank You!");
        }
    };
    xhttp.send();
两个代码段中的路径相同:,因此问题可能不在于路径

你知道问题出在哪里吗?

为什么

var params = "email=" + email + "&company=" + company + "&subject=" + subject + "&text=" + text
...
data: '{"data":"' + params + '"}' ,
试试看

data: { email: email, company: company, subject: subject, text: text }
在Node.js中

req.param['email'] ... etc

这里的问题是您正在进行一个JSONP调用,这是一个GET请求。您不能将JSONP作为POST。查看屏幕截图中的请求,您可以看到它是一个GET

dataType: "jsonp",   <-- changes the POST to a GET
dataType:“jsonp”,试试这个(需要删除jsonp和数据):
函数addFormToDB(电子邮件、公司、主题、文本){


}

您在调试器的
网络
选项卡上看到什么了吗?从哪里调用
addFormToDB()
?我们能看到客户端HTML吗?我编辑了原始的帖子。404意味着它在服务器上找不到路径/文件。。。。因此,我将开始看,似乎有一个问题与您的路由,因为你得到了一个404。您是否尝试过使用REST客户端(如postman)访问端点?为什么要在帖子上使用querystring?毫无意义。你不能有jsonp和post!Ajax和普通JS示例并不相同。接下来,为什么一个有端口而另一个没有?
   $.ajax({
      url: 'http://127.0.0.1/submit',
      type: 'POST',
      data: {email: email, company: company, subject: subject} ,
      xhrFields: {
         withCredentials: false
      },
      success: function(data) {
         alert("success");
      },
      error: function(result) {
         alert("error");
     }
   });