使用JSONP进行Ajax调用不起作用。没有提供回调

时间:2014-09-02 13:43:31

标签: jquery ajax asp.net-web-api jsonp

我正在尝试这样的api调用:

 var url = 'http://[api link]?callback=?';

$.ajax({
    type: 'GET',
    url: url,
    async: true,
    xhrFields: {
        withCredentials: true
    },
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(json) {
        console.dir(json.sites);
    },
    error: function(e) {
        console.log(e.message);
    }
});

但在浏览器中,它始终以以下格式显示:Error Image

在小提琴手中,我得到以下错误:

[InvalidOperationException: A callback parameter was not provided in the request URI.]
WebApiContrib.Formatting.Jsonp.JsonpMediaTypeFormatter.GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType) + 320
System.Net.Http.Formatting.DefaultContentNegotiator.Negotiate(Type type, HttpRequestMessage request, IEnumerable` 1 formatters) + 199
System.Web.Http.Results.NegotiatedContentResult` 1.Execute(HttpStatusCode statusCode, T content, IContentNegotiator contentNegotiator, HttpRequestMessage request, IEnumerable` 1 formatters) + 103
System.Net.Http.HttpRequestMessageExtensions.CreateResponse(HttpRequestMessage request, HttpStatusCode statusCode, T value, HttpConfiguration configuration) + 235
System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Func` 2 errorCreator) + 298
System.Web.Http.ExceptionHandling.DefaultExceptionHandler.Handle(ExceptionHandlerContext context) + 152
System.Web.Http.ExceptionHandling.DefaultExceptionHandler.HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken) + 12
System.Web.Http.ExceptionHandling. & lt;
HandleAsyncCore & gt;
d__0.MoveNext() + 136
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) + 144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) + 84
System.Runtime.CompilerServices.TaskAwaiter` 1.GetResult() + 49
System.Web.Http. & lt;
SendAsync & gt;
d__0.MoveNext() + 1584
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) + 144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) + 84
System.Runtime.CompilerServices.TaskAwaiter` 1.GetResult() + 49
System.Web.Http.WebHost. & lt;
ProcessRequestAsyncCore & gt;
d__0.MoveNext() + 624
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) + 144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) + 84
System.Web.TaskAsyncHelper.EndTask(IAsyncResult ar) + 98
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean & amp; completedSynchronously) + 288

在调用的.js文件中,我定义了一个 功能如下:

jsonCallback = function(data) {

}

我在这里做错了什么?

编辑:

ajax错误的屏幕截图: enter image description here

编辑:

WebapiConfig.cs

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

    //add support for JSONP
    var formatter = new JsonpMediaTypeFormatter(jsonFormatter, "cb");
    config.Formatters.Insert(0, formatter);

    //Add Support CORS
    config.EnableCors();

1 个答案:

答案 0 :(得分:0)

如果您尝试这样做会返回什么?

$.ajax({
  url: 'http://[api link]', // without the callback at the end
  type: 'GET',
  dataType: 'jsonp',
  success: function (result) {
     $('#jsonp').html(result);
  }
});