简体   繁体   中英

Javascript AJAX not working on ASP.NET C# and returns a 404

I have a front end code which sends a POST request to the server and returns nothing as the server needs the information.However in Chrome's log i see a 404 Thanks,

Failed to load resource: the server responded with a status of 404 (Not Found)

The following code sends the server a request:

 var data = "hi" var theIds = JSON.stringify(data); var UrlFixer = '/Process/Complete'; // Make the ajax call $.ajax({ type: "POST", url: UrlFixer, contentType: "application/json; charset=utf-8", data: { ids: theIds }, dataType: "json", success: function (result) { alert('Yay! It worked!'); }, error: function (result) { alert('Oh no :('); } }); 
Backend(C#):

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Quiz3 { public class Process { [HttpPost] public static void Complete(string[] ids) { String[] a = ids; } } } 

You can get help from the code below to send data.

You do not have to submit a string item in the form { ids: theIds } You must change the form to JSON.stringify(data) .

var data = "Your Name";
$.ajax({
    url: '/home/Complete',
    type: 'POST',
    data: JSON.stringify(data),
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        alert(data.success);
    },
    error: function () {
        alert("error");
    }
});

And your class should not be simple. You must have a controller class

Backend:

public class HomeController : Controller
{

    [HttpPost]
    public void Complete(string Name)
    {
        //Code ...
    }
}

也许将您的ID作为ID列表发送,而不对列表进行字符串化

public static void Complete(List<T> Ids)

I managed to get this to work with MVC Empty template by the following method:

Firstly a controller is made(inside the controller folder) and its named 'SubmitController'.Now at first i thought that naming it controller at the end is not important but it turns out it is and the URL would be '/Submit' and not '/SubmitController'. Inside the C# file a function is made and i called it 'Process' with an POST attribute(HttpPost).The URL now would be '/Submit/Process'.In my case i needed to pass an array from the client to server and that is the extra piece of code used in C# to process it and display on the Output page of Visual studio.

SubmitController.cs

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

    namespace MvcApplication1.Controllers
    {
        public class SubmitController : System.Web.Mvc.Controller
        {
            [HttpPost]
            public PartialViewResult Process(string myArray)
            {
              String[] Items = myArray.Split(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries);
              //print
               Items.ToList().ForEach(i => System.Diagnostics.Debug.WriteLine(i.ToString()));

                return null;
            }
        }
    }

SendEvent.js

// Make the ajax call
            var myArray = data; 
            $.ajax({
                type: "POST",
                url: '/Submit/Process',
                data: { 'myArray': myArray.join(',') }, 
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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