【问题标题】:Querying Microsoft Dynamics NAV 2013 Odata service as JSON Format以 JSON 格式查询 Microsoft Dynamics NAV 2013 Odata 服务
【发布时间】:2016-03-09 19:47:33
【问题描述】:

我已阅读here,Odata Web 服务也支持 JSON 格式。但是我怎样才能得到呢?

当我发送请求时,我只得到以下格式> application/atom+xml

【问题讨论】:

    标签: json web-services odata microsoft-dynamics


    【解决方案1】:

    试试这样的:

    $.ajax({
           type: "GET",
           contentType: "application/json; charset=utf-8",
           datatype: "json",
           url: odataSelect,
           beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
           success: function (data, textStatus, XmlHttpRequest) 
               { 
                   ProcessReturnedEntities(data.d.results); 
                   ProcessReturnedEntity(data.d);
               },
           error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
       });
    

    有关完整示例,请参阅此site

    【讨论】:

    • 谢谢!我需要将以下标头添加到 GET-Request: Accept: "application/json"
    • 这个调用跨域怎么用jsonp做的?
    【解决方案2】:

    对于带有 HTML 和 JS 的 Windows 8 应用程序中的 WinJS,其如下:

    WinJS.xhr({
        type: "GET",
        datatype: "json",
        url: 'http://localhost:7048/DynamicsNAV70/OData/P_21/',
        headers: {
            "Content-type": "application/json; charset=utf-8", "Accept": "application/json" },
        }).done(function (data, textStatus, XmlHttpRequest)  {
             console.log();
        },
        function (err) {
            console.log();
        });
    

    请注意标题的不同定义。值完全相同。

    【讨论】:

      【解决方案3】:

      要使用 JSON 而不是 XML 与您的 OData Web 服务进行通信,您实际上只需要设置以下两个标头:

      • Accept: application/json
      • Content-Type: application/json; charset=utf-8

      或者,您也可以将 ?$format=json 放在 URL 的末尾。

      无论您使用哪种编程语言与 Microsoft Dynamics NAV 通信,这都是事实。它同样适用于 JavaScript、JAVA、Python、Ruby、PHP,...


      演示代码

      这是从 PHP 执行基本 GET 请求的方法:

      $ch = curl_init(); 
      
      curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'1\')'); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
      curl_setopt($ch, CURLOPT_POST, false);  
      
      curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
              'Accept: application/json',          
              'Content-Type: application/json; charset=utf-8',
      ]);   
      
      $response = json_decode(curl_exec($ch), TRUE);
      echo json_encode($response, JSON_PRETTY_PRINT);
      // Close handle
      curl_close($ch);
      

      这是从 PHP 发出基本 POST 请求的方法:

      $ch = curl_init(); 
      
      curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer'); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
      curl_setopt($ch, CURLOPT_POST, true);  
      
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
          "Name" => "This is a test customer",
          ...
      ]));
      
      curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
              'Accept: application/json',          
              'Content-Type: application/json; charset=utf-8',
      ]);   
      
      $response = json_decode(curl_exec($ch), TRUE);
      echo json_encode($response, JSON_PRETTY_PRINT);
      curl_close($ch);
      

      以下是如何从 PHP 发出基本的 PATCH 请求:

      $ch = curl_init(); 
      
      curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'1\')'); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
      curl_setopt($ch, CURLOPT_POST, true);  
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
      
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
          "Name" => "This is a test customer",
          ...
      ]));
      
      curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
              'Accept: application/json',          
              'Content-Type: application/json; charset=utf-8',
              'If-Match: W/"\'' .  $etag . '\'"'
              // You can get your etag value by doing a get request first
      ]);   
      
      $response = json_decode(curl_exec($ch), TRUE);
      echo json_encode($response, JSON_PRETTY_PRINT);
      curl_close($ch);
      

      以下是如何从 PHP 发出基本的 DELETE 请求:

      $ch = curl_init(); 
      
      curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'1\')'); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
      curl_setopt($ch, CURLOPT_POST, true);  
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
      
      curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
              'Accept: application/json',          
              'Content-Type: application/json; charset=utf-8',
              'If-Match: W/"\'' .  $etag . '\'"'
              // You can get your etag value by doing a get request first
      ]);   
      
      $response = json_decode(curl_exec($ch), TRUE);
      echo json_encode($response, JSON_PRETTY_PRINT);
      curl_close($ch);
      

      注 1

      如果您需要创建/更新数据,请不要忘记对您的 POST 字段进行 Json 编码:

      curl_setopt($ch, CURLOPT_POST, true);  
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
          "Name"=> "This is the name of my new customer"
      ]));
      

      使用查询字符串或数组代替会产生错误An error occurred while processing this request.,这可能会让您困惑很长时间...


      注2

      对于那些不喜欢处理原始 cURL 请求的人,我刚刚上传了一个基本的 OO 包装类,您可以在 this gist 找到它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多