Ajax 发布到控制器 404

用户3046358

我有一个 ajax 调用发布到控制器。

当我点击一个销售按钮时,一个提示允许用户输入他们想要销售的游戏名称,该名称保存在 Video Game Store 数据库中。

但是,当调用 SellFunction 时,我收到 404 错误。

这是 JavaScript 代码:

function SellFunction() {
var name = prompt('Please enter the game you are selling us:');
alert("Thank you for your business!");

var RandomID = Math.random
RandomID *= 20;
Math.floor(RandomID);

var GameObject = {VideoID: RandomID, Price: 20, Name: name } //now we have to basically just send something easy

//ajax post store name in video game model for the store
$.ajax({
    type: "POST",
    data: JSON.stringify(GameObject),
    url: "index/videogamesale",
    dataType: 'json',
    contentType: false,
    processData: false,
    success: function (response) {
        if (response != null && response.success) {
            alert(response.responseText);
        } else {
            // DoSomethingElse()
            alert(response.responseText);
        } 
    }

});

}

这是我收到的错误:404 错误

这是我的控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace VideoGameStore.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult VideoGameSale(VideoGame newgame)
        {
            using (var db = new VideoGameModel())
            {

                db.Games.Add(newgame);
                db.SaveChanges();

                foreach (var VideoGame in db.Games)
                {
                    Console.WriteLine(VideoGame.Name);
                }

               return Json(new { success = true, responseText = "Your 
               message successfuly sent!" }, JsonRequestBehavior.AllowGet);

            }
        }

    }

}

这是视频游戏模型代码:

using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Infrastructure;

namespace VideoGameStore
{
    public class VideoGameModel : DbContext
    {
        public DbSet<VideoGame> Games { get; set; }
    }

    public class VideoGame
    {
        public int VideoID { get; set; }
        public int Price { get; set; }
        public string Name { get; set; }
    }
}

这是表的图片:dbo.VideoTD

我在网上搜索过,但问题仍然像以往一样难以捉摸。任何帮助将不胜感激。谢谢你。

瓦贾哈特·阿里·阿比德

我相信您应该将 ajax 请求更改为此,它会完美解决

$.ajax({
    type: "POST",
    data: JSON.stringify(GameObject),
    url: "Home/VideoGameSale",
    method: "POST",
    dataType: 'json',
    contentType: false,
    processData: false,
    success: function (response) {
        if (response != null && response.success) {
            alert(response.responseText);
        } else {
            // DoSomethingElse()
            alert(response.responseText);
        } 
    }});

路由器会将其映射到 Controller Home 和 Action VideoGameSale。由于您的操作 VideoGameSale 需要 HTTPPOST 请求,因此它会解决方法的原因:“POST”语句。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试发布到控制器时收到 404 错误

通过ajax发布到控制器的模型为空

从局部视图到控制器的ajax发布为null

使用jquery ajax发布到CakePHP控制器

ajax不会将文件发布到控制器mvc

将ajax数据发布到spring控制器

MVC Ajax在单击事件中发布到控制器

无法发送电子邮件 - Azure MVC 站点在尝试通过控制器将表单发布到 ajax 请求时从 jquery.min.js:2 返回 404

Ajax发布到ASP.net MVC控制器-对象属性为null

在 .net core 中发布到控制器时,无法让 .ajax 给出一个值

如何通过JQuery ajax调用将dataTable的选定行的数据发布到php控制器

使用AJAX将传递参数发布到Rails控制器

如何使用AJAX将数据发布到ASP.NET MVC控制器?

将数组和表单数据发布到控制器-MVC Ajax

ASP.NET 5 / MVC 6 Ajax将模型发布到控制器

Ajax中的日期将数据发布到MVC控制器

ajax 发布到 MVC 控制器失败,400 错误请求

Ajax发布不会将任何数据传递到MVC控制器

使用Ajax将表单数据发布到控制器方法

使用Ajax将JSON数据发布到MVC控制器

使用AJAX将数据发布到控制器不起作用,Code Igniter

使用AJAX将表单数据发布到Fw / 1(Coldfusion)控制器功能

jQuery Ajax发布到MVC控制器不起作用(返回null)

从ajax到控制器的ASP.Net Core发布列表

如何在API中使用Ajax将Laravel表单数据发布到控制器

Laravel中的AJAX函数未将表单数据发布到控制器

Laravel Ajax表单数据未发布到控制器

无法通过AJAX将数据发布到Grails中的控制器的Action中

将 AJAX 字符串发布到 MVC 控制器