相关疑难解决方法(0)

Web Api参数始终为null

当我用下面的ajax调用下面的Post方法时,为什么参数总是为null?

public IEnumerable<string> Post([FromBody]string value)
{
    return new string[] { "value1", "value2", value };
}
Run Code Online (Sandbox Code Playgroud)

这是通过ajax调用Web API方法:

  function SearchText() {
        $("#txtSearch").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "api/search/",
                    data: "test",
                    dataType: "text",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

asp.net ajax asp.net-web-api

35
推荐指数
2
解决办法
4万
查看次数

使用jQuery发布到ASP.Net webapi

遇到麻烦:

我做了这个简单的测试,警报会弹出文本"test return simple":

jQuery帖子:

$.post("http://www.localhost/webapi/api/corkboard/test/", jsonData)
            .done(function(data){
                alert(data);
        });
Run Code Online (Sandbox Code Playgroud)

Asp.Net WebAPI:

[HttpPost]
public string test()
{        
    return "test return simple";
}
Run Code Online (Sandbox Code Playgroud)

但是当我通过添加参数来更改WebAPI时:

public string test(string JSONData)
    {
        var jData = Json.Decode(JSONData);
        return "test return: " + jData.Filter;            
    }
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

"没有找到与请求URI匹配的HTTP资源' http://www.localhost/webapi/api/corkboard/test/ '

坚持并会感激任何想法...谢谢!

jquery post asp.net-web-api

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

asp.net-web-api ×2

ajax ×1

asp.net ×1

jquery ×1

post ×1