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

时间:2013-04-09 20:17:30

标签: json web-services odata microsoft-dynamics

我已阅读here,Odata Web Service也支持JSON格式。但我怎么能得到它?

当我发送请求时,我只能获得以下格式>应用/原子+ xml的

3 个答案:

答案 0 :(得分:4)

尝试类似的东西:

$.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

答案 1 :(得分:2)

对于带有HTML和&amp ;;的Windows 8应用程序内的WinJS JS其内容如下:

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();
    });

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

答案 2 :(得分:1)

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

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

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

无论您使用何种编程语言与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找到它。