Jquery AJAX JSONP 不工作

Jquery AJAX JSONP not working

首先,我是 WebAPICore 和 JSONP 的新手,正在尝试从 WebAPICore 获取 JSON 数据。

我已经构建了 API 测试 returns JSON 对象,它在浏览器中运行良好。我也确实配置了 CORS 以允许跨源访问。

我做了测试 JSONP 调用,如图所示 https://www.sitepoint.com/jsonp-examples/

示例中显示的工作代码

  $.getJSON("https://api.github.com/users/jeresig?callback=?", function (json) {
      console.log(json);
  });

即使 return JSON 数据返回也没有工作

  $.getJSON("http://localhost:1569/offerings/5?callback=?", function (json) {
      console.log(json);
  });

我在调用我的服务时收到 'Unexpected token' 错误。

我不明白为什么它不能只对我的服务有效。请参阅下图,了解 return jsons 和 Chrome 控制台中的调试。

GitHub API 和我的 API 之间的唯一区别是 JSONs 的格式。

我是否需要在我的服务中对 JSONP 的 JSON 数据进行特殊格式化或编码?

我的测试代码,其中 returns JSON 来自 Web 的对象APICore

    [HttpGet("{id}")]
    public Offering Get(int id)
    {
        return new Offering()
        {
            OfferingID = id
            , CourseName = "TestCourse"
            , CourseCode = "TestCode"
            , Description = "My Description"
        };
    }

如果有人遇到过这个错误...这里是工作代码:

$.ajax({
                url: 'http://localhost:1569/offerings/5',
                success: function (result) {
                    console.log(result);
                }
            });

只需将 $.getJSON 更改为传统的 $.ajax 调用