weixin_33721344 2017-07-16 19: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 20: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.

    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan