为什么这个基本的ajax调用会给我400而不向控制器开火? [英] Why is this basic ajax call giving me a 400 and not firing to the controller?

查看:69
本文介绍了为什么这个基本的ajax调用会给我400而不向控制器开火?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说只是最新的点网核心2网络应用程序中的一个问题,它可以在以前的点网应用程序中正常运行.

This is only a problem for me in my newest dot net core 2 web app, it works in previous dot net apps with no issue.

我有一个自定义javascript文件,其中包含指向我的网站托管在其中的各种服务器(主要是dev,test,live)的路径.

I have a custom javascript file with paths to various servers that my site is hosted on(mainly dev, test, live).

我的ajax调用如下所示:

My ajax call looks like the following:

var gameid = '@Html.Raw(Model.Id)';
    $.ajax({
        type: 'GET',
        url: url() + "UserGames/HasGame?id=" +gameid,
        cache: false,
        success: function (data) {
            console.log(data);
        },
        error: function (req, status, err) {
            console.log('Something went wrong', status, err);
        }
    });

有问题的url()只是在测试时看着我的本地服务器(*请注意,此端口已被编辑,这是正确的):

The url() in question is simply looking at my local server while testing(*please note, this port has been edited, it is correct):

function url() {
return "https://localhost:4432/";
}

该错误不会触发,因为它甚至没有达到所使用的控制器功能.

The error is not firing as it isnt even reaching the controller function being used.

我对此到底为什么不起作用感到困惑,我可以理解是否返回了某种错误,但是我在所有应用程序中都使用过ajax调用,直到使用core 2时才完全没有此问题.

I'm absolutely bemused as to why this isnt working, i could understand if i was getting some sort of error back, but i have used ajax calls in all of my apps and not had this problem at all until using core 2.

是否有任何可能影响您使用ajax调用方式的更改?

Has anything changed that may have affected the way you use ajax calls?

试图触发的方法,但我提到过,它没有命中控制器(注意:该方法中确实有代码,首先它并没有命中控制器)

Method that is trying to fire, but i mentioned, it isnt hitting the controller(NOTE: the method does have code in it, it just isnt hitting the controller in the first place)

[HttpGet]
[ValidateAntiForgeryToken]
public async Task<IActionResult> HasGame(int id)
{
    //stuff in here
}

推荐答案

如果还有其他人遇到此问题,我设法通过对呼叫和控制器执行以下操作来解决此问题:

If anyone else comes across this issue, i managed to fix it by doing the following to both the call and the controller:

type: 'GET',
url: '@Url.Action("HasGame", "UserGames")',
data: {id : gameid},

核心2中的控制器似乎根本不喜欢您声明[HttpGet],所以我也删除了它.

And the controller in core 2 does not seem to like you declaring [HttpGet] at all, so i removed that also.

感谢所有提供的帮助.

这篇关于为什么这个基本的ajax调用会给我400而不向控制器开火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆