我在这里读到,Odata Webservice 也支持 JSON 格式。但我怎么能得到呢?
当我发送请求时,我只得到以下格式> application/atom+xml
我在这里读到,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
或者,您也可以将其放在?$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);
如果您需要创建/更新数据,请不要忘记对您的 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.
,这可能会让您困惑很长时间......
对于那些不喜欢处理原始 cURL 请求的人,我刚刚上传了一个基本的 OO 包装类,您可以在这个 gist中找到它。