Ajax发布到控制器404 [英] Ajax Post to Controller 404

查看:81
本文介绍了Ajax发布到控制器404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ajax call posting to a controller.

当我单击出售"按钮时,提示用户输入要出售的游戏名称,该名称保存在视频游戏商店数据库中.

When I click on a sell button a prompt allows the user to type in the name of a game they want to sell, which is saved in the Video Game Store database.

但是,当调用SellFunction时,出现404错误.

When the SellFunction is called, however, I'm getting a 404 error.

这是JavaScript代码:

Here is the JavaScript Code:

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);

            }
        }

    }

}

这是VideoGame模型代码:

Here is the VideoGame Model Code:

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

我已经在网上搜索了,但是问题仍然像从前一样难以捉摸.任何帮助将不胜感激.谢谢.

I've searched the web, but the problem remains as elusive as ever. Any help would be greatly appreciated. Thank you.

推荐答案

我相信您应该对此更改ajax请求,这样效果会很好

I believe you should change ajax request to this and it'll work out perfectly

$.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和动作VideoGameSale.并且由于您的动作VideoGameSale需要HTTPPOST请求,因此可以解决方法的原因:"POST"声明.

And router will map it to Controller Home and action VideoGameSale. And since your action VideoGameSale expects a HTTPPOST request it'll work out cause of method: "POST" statement.

这篇关于Ajax发布到控制器404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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