簡體   English   中英

以Microsoft JSON格式查詢Microsoft Dynamics NAV 2013 Odata服務

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

我在這里讀到,Odata Webservice也支持JSON格式。 但是我怎么能這樣呢?

當我發送請求時,我只獲得以下格式> application / atom + xml

嘗試類似的東西:

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

有關完整示例,請參閱此站點

對於使用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();
    });

請注意標題的不同定義。 值完全相同。

要使用JSON而不是XML與OData Web服務進行通信,您實際上只需要設置以下兩個標頭:

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

或者,您也可以在URL的末尾放置?$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包裝器類,您可以在此要點找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM