为什么Ajax不发送数据? [英] Why isn't Ajax sending data?

查看:87
本文介绍了为什么Ajax不发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果输入变量(a)未设置为Null,则以下Ajax代码甚至不会触发我的操作。

The following Ajax code doesn't even trigger my action if the input variable (a) isn't set to Null.

Ajax代码:

var ab = "Chocolate Smoothies ya know?";

$("#customerSubmit").submit(function (e) {

    e.preventDefault();

    $.ajax({
        url: "/api/Booking/lander",
        method: "post",
        data: { a: ab }
    })
});

以下我的控制器代码:

[HttpPost]
public void lander(string a)
{
    System.Diagnostics.Debug.WriteLine(a);
}

当我没有将它设置为null时,收到的输入为空。

And when I do not set it to null, the input received is null.

触发断点时的屏幕截图:

screenshot when the breakpoint is triggered:

我使用了类型/方法/等等。似乎没什么用。

I've used type/method/etc.. Nothing seems to work

更新:

我甚至尝试了以下但没有用:

I even tried the following but no use:

更新2:

即使以下情况也不起作用,以下其中一项也给了我 - >无法加载资源:服务器响应状态为405(方法不允许)

Even the following didn't work and one of the following also gave me -> "Failed to load resource: the server responded with a status of 405 (Method Not Allowed)"

var ab = 'Chocolate Smoothies ya Know?';
$.ajax({
    url:"/api/Booking/lander", 
    data: {'': ab}
});

使用

public string lander([FromUri]string asx) ////and asx = "" and /// asx = null

更新3

发生了一件非常奇怪的事情,我使用了以下代码:

Something extremely weird happened, I used the following code:


和我得到了下载机翼错误:

and I got the following error:

当我使用


////(字符串a =)

////(string a = "")

由于某种未知原因触发了我的操作,发生了以下情况。

it triggered my action for some unknown reason and the following happened.

以下是我的WebApiConfig代码:

The following is my WebApiConfig code:

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

namespace HotelManagementSystem
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            //routes.MapHttpRoute("RestApiRoute", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" }); //this replaces your current api route


            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

更新4:

即使以下设置也不起作用:

Even the following set up did not work:

推荐答案

您的RouteConfig.cs文件中定义了默认路由

you have a default route defined in your RouteConfig.cs file

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new [] { "YourNameSpace.Controllers" }
        );

如果您将签名中的a更改为id并通过ajax发送id你会成功,但如果你想在post方法中有多个参数或不同的命名尝试定义新的路由。

if you change "a" in your signature to "id" and send "id" through your ajax you will succeed but if you want to have multiple parameters in your post method or different naming try to define new routes.

这篇关于为什么Ajax不发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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