WebAPI:请求URI中未提供回调参数

时间:2014-10-05 08:53:59

标签: json jsonp asp.net-web-api fiddler

我正在使用fiddler在我的API中执行post方法我得到错误"请求URI中没有提供回调参数。"。但是,这适用于get方法。

我已经看到了这个问题的几个答案,并且根据错误我需要指定一个回调参数。但是,我不知道如何使用fiddler这样做。

回应Can I make a jQuery JSONP request without adding the '?callback=' parameter in URL?的答案之一。我在小提琴手中尝试了以下内容,但我得到同样的错误..

url:http://velopoint-api.localhost.dev/api/v1/tasks?callback=foo

头:

User-Agent: Fiddler
Host: velopoint-api.localhost.dev
ContentType: application/json; charset=utf-8
Authorization: basic "UNQUOTED"
Content-Length: 47
jsonp: true
jsonpCallback: jsonCallback
dataType: jsonp

请求正文

{ "Title":"New Task", "DueDate":"20-jul-2014" }

启动

        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.

            // Change Formater to use CamelCasePropertyNamesContractResolver

            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            /* Support JsonP */
            //register JSONP media type formatter
            config.Formatters.Insert(0, new JsonpMediaTypeFormatter(jsonFormatter));

            config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
            config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            ...
}

1 个答案:

答案 0 :(得分:1)

玩了一会儿之后,我终于发现它实际上没有路由到post方法。

我的标题现在是

User-Agent: Fiddler
Host: velopoint-api.localhost.dev
Authorization: basic UNQUOTED
Content-Length: 224
Content-Type: application/json

我通过在Post方法中指定Route属性并将空字符串传递给pattern参数来解决问题,在Get和Post上都有(因为我已经在类上指定了RoutePrefix属性。

[RoutePrefix("api/v1/tasks")]
[VeloPointAuthorise(perUser: true)]
public class TaskController : BaseApiController
{

    [HttpGet]
    [Route(template:"", Name = "TaskRoute")]
    public HttpResponseMessage Get(int page = 0)
    {
       ....
    }

    [HttpPost]
    [Route(template:"")]
    public HttpResponseMessage Post([FromBody] OrganiserTaskModel model)
    {
       ....        
    }