简体   繁体   English

通过jQuery发送ajax请求不起作用

[英]Sending ajax request by jquery doesn't working

I'm trying to send ajax request to api. 我正在尝试将ajax请求发送到api。 My code is 我的代码是

$(".doit").click(function(){
    console.log("GG");
    $.ajax({
        type: 'POST',
        url: "localhost:8000/api/get-user/1",
        data: {id:1},
        success: function(res) { 
            console.log(res);
        }
    });
})  

It's just a simple code. 这只是一个简单的代码。 Everyone know. 大家都知道 But it works in "console.log("GG"). Not working in ajax part. 但是它可以在“ console.log(” GG“)中工作,而不能在ajax部分中工作。
I monitored the network traffic by firefox, but I ddin't see any network traffic. 我通过firefox监视了网络流量,但没有看到任何网络流量。
Do you have any idea about that case? 您对此情况有任何想法吗?

This is because you are specifying the port number, and also not including the scheme. 这是因为您指定的是端口号,也不包括方案。

Simply removing your domain should be enough: 只需删除您的域就足够了:

$(".doit").click(function(){
    console.log("GG");
    $.ajax({
        type: 'POST',
        url: "/api/get-user/1",
        data: {id:1},
        success: function(res) { 
            console.log(res);
        }
    });
}) 

Update your ajax URL as below 如下更新您的ajax URL

url: "http://localhost:8000/api/get-user/1",

or 要么

url: "api/get-user/1",

Hope this will fix your issue 希望这能解决您的问题

The problem is that you do not specify the URL scheme. 问题是您没有指定URL方案。 So, instead of localhost:8000/api/get-user/1 you should use //localhost:8000/api/get-user/1 , or better specify just the relative path /api/get-user/1 . 因此,而不是localhost:8000/api/get-user/1 ,你应该使用//localhost:8000/api/get-user/1 ,或更好只指定相对路径/api/get-user/1

I think because your url in the same url as you want to make request and that didn't make you will see the network traffic. 我认为是因为您的网址与您要发出请求的网址相同,但这并没有使您看到网络流量。 just using the different url will show the request ajax from the network traffic. 仅使用不同的url将显示来自网络流量的请求ajax。

just try to change your url to this: https://jsonplaceholder.typicode.com/posts and read about this further information: https://github.com/typicode/jsonplaceholder#how-to 只需尝试将您的URL更改为此: https : //jsonplaceholder.typicode.com/posts并阅读有关此进一步的信息: https : //github.com/typicode/jsonplaceholder#how-to

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

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