weixin_33721344 2017-07-16 11:40 采纳率: 0%
浏览 48

为什么Ajax不发送数据?

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

Ajax code:

var ab = "Chocolate Smoothies ya know?";

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

    e.preventDefault();

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

The following my controller code:

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

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

screenshot when the breakpoint is triggered:

enter image description here

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

Update:

I even tried the following but no use:

Update 2:

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

With

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

enter image description here

enter image description here

Update 3

Something extremely weird happened, I used the following code:

enter image description here enter image description here

and I got the following error:

enter image description here

and when I used

////(string a = "")

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

enter image description here

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

Update 4:

Even the following set up did not work:

enter image description here

enter image description here

enter image description here

展开全部

  • 写回答

2条回答 默认 最新

  • helloxielan 2017-07-16 12:02
    关注

    You need to set contentType and stringify the data you want to send to controller. So the data you will send will be:

    var postData = JSON.stringify({'a':ab});
    

    So the resulting code should look like:

    var ab = 'Chocolate Smoothies ya Know?';
    var postData = JSON.stringify({'a':ab});
    $.ajax({
        url: "/api/Booking/lander", 
        method: "POST",
        contentType: "application/json",
        data: postData
    });
    

    You may optionally want to set dataType: "json" too. You do not need to specifiy any lookup locations (uri, body) for a parameter in lander method.

    评论
  • weixin_33691817 2017-07-16 12:16
    关注

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

    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.

    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部