【问题标题】:Ajax Call to Retrieve oData from REST API从 REST API 检索 oData 的 Ajax 调用
【发布时间】:2018-05-02 21:41:56
【问题描述】:

我正在尝试使用 Ajax 从 oData REST API 获取数据。 Ajax 看起来像这样:

$(document).ready(function () {
        $.ajax({
            url: "http://localhost:52139/odata/WEB_V_CIVIC_ADDRESS?key=10064450", datatype: 'json',
            success: function (oResult)
            {
                alert("good");
                $('.accountNumber').append(data.accountNumber);
                $('.civicaddress').append(data.civicaddress);
            },
            error: function(data)
            {
                alert('bad');
            }
        });
    });

URL 调用得很好,但我总是收到“坏”警报,所以调用会引发错误。网址正在返回:

{"@odata.context":"http://localhost:52139/odata/$metadata#WEB_V_CIVIC_ADDRESS/$entity","@odata.type":"#Values.Classes.Entities.AccountAddress","accountNumber":10064450,"rowNumber ":0,"civicaddress":"123 Fake St"}

那么如何从 ajax 调用中获取 accountNumber 和 civicaddress?

谢谢。

【问题讨论】:

    标签: ajax rest odata


    【解决方案1】:

    如果有人感兴趣,解决方案是在我的 web.config 中为 REST 服务添加以下内容:

    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
        </customHeaders>
      </httpProtocol>
    </system.webServer>
    

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 2021-09-08
      • 2016-11-14
      • 2011-08-15
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多