【问题标题】:Error in Consuming Web Api Post using Ajax使用 Ajax 使用 Web Api Post 时出错
【发布时间】:2013-12-30 16:23:39
【问题描述】:

我在 MVC 中有一个 WEB API, 我正在尝试使用 Ajax 在 jquery 中使用相同的内容

$.ajax({
            type: "POST",
            dataType: "jsonp",
            contentType: "application/javascript",
            data: data,
            async: false,
            url: "http://localhost:5133/api/Budget_Tracking",
            success: function (jsonData) {
                console.log(jsonData);
            },
            error: function (request, textStatus, errorThrown) {
                console.log(request.responseText);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });

执行此代码时出现错误:: 错误是

"jQuery1102043945081951096654_1388391275993 was not called"

【问题讨论】:

  • http://localhost:5133/api/Budget_Tracking 你是从这里得到json 吗?服务器是否已配置为使用 cors 发送 json。
  • 是的,它正在发送 JSON
  • contentType: "application/javascript", 尝试删除它。或者改成这个contentType: "application/json",
  • 也试过这个 :: 不工作

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


【解决方案1】:

option 添加到$.ajax()

jsonpCallback: 'myCallback',

还将url: 更改为"http://localhost:5133/api/Budget_Tracking?callback=?",

并创建一个function 喜欢,

function myCallback(){
   //your code with callback
}

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 2016-06-26
    • 1970-01-01
    • 2017-07-26
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    相关资源
    最近更新 更多