简体   繁体   English

以Microsoft JSON格式查询Microsoft Dynamics NAV 2013 Odata服务

[英]Querying Microsoft Dynamics NAV 2013 Odata service as JSON Format

i have read here , that the Odata Webservice also supports the JSON format. 我在这里读到,Odata Webservice也支持JSON格式。 But how can I get that? 但是我怎么能这样呢?

When I send a request i only get the following format> application/atom+xml 当我发送请求时,我只获得以下格式> application / atom + xml

Try something like that: 尝试类似的东西:

$.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); }
   });

See this site for a complete example. 有关完整示例,请参阅此站点

For WinJS inside Windows 8 apps with HTML & JS its the following: 对于使用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();
    });

Please note the different definition of the header. 请注意标题的不同定义。 The values are exactly the same. 值完全相同。

To communicate with your OData web services using JSON instead of XML, you really only need to set the following two headers : 要使用JSON而不是XML与OData Web服务进行通信,您实际上只需要设置以下两个标头:

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

Alternatively, you could also put ?$format=json at the end of your URL. 或者,您也可以在URL的末尾放置?$format=json

This it true no matter what programming language you're using to communicate with Microsoft Dynamics NAV. 无论您使用何种编程语言与Microsoft Dynamics NAV进行通信,都是如此。 It works the same for JavaScript, JAVA, Python, Ruby, PHP, ... 它适用于JavaScript,JAVA,Python,Ruby,PHP,...


Demo code 演示代码

Here's how to do a basic GET request from 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);

Here's how to do a basic POST request from PHP : 这是从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);

Here's how to do a basic PATCH request from PHP : 以下是如何从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);

Here's how to do a basic DELETE request from PHP : 以下是如何从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);

Note 1 注1

If you need to create / update data, don't forget to Json-encode your POST fields : 如果您需要创建/更新数据,请不要忘记对您的POST字段进行Json编码:

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

Using a query string or array instead will generate an error An error occurred while processing this request. 使用查询字符串或数组将生成错误An error occurred while processing this request. , which could leave you puzzled for quite a while... ,这可能会让您困惑很长一段时间...


Note 2 笔记2

For those who don't like working with raw cURL requests, I just uploaded a basic OO wrapper class, which you can find at this gist . 对于那些不喜欢使用原始cURL请求的人,我刚刚上传了一个基本的OO包装器类,您可以在此要点找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM