使用PHP,我试图捕获REST调用中返回的错误描述并在javascript警报中将其抛出。
以下是XML错误响应:
<?xml version="1.0" encoding="utf-8"?>
<get_response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<number_results>0</number_results>
<search_criteria xsi:type="Forte.RestApi.Common.Models.v3.SearchRestrictions">
<page_size>50</page_size>
<page_index>0</page_index>
<home_organization_id>org_334277</home_organization_id>
</search_criteria>
<response>
<environment>live</environment>
<response_desc>Error[1]: The content in the query string produced errors while parsing. Check that the content is correctly formatted. Error[2]: Error converting value '2018-02-31'. Field: 'end_au_updated_date'</response_desc>
</response>
<links>
<self>https://api.forte.net/v3/paymethods/?filter=start_au_updated_date+eq+%272018-02-01%27+and+end_au_updated_date+eq+%272018-02-31%27</self>
</links>
</get_response>
以下是我的工作原理:
$response = curl_exec($ch);
if (strpos($response, 'Error') !== false) {
echo "<script language='javascript'>alert",($response["response"]["response_desc"]),";</script>";
die;
}
我想要捕获的值是&#34; response_desc&#34;字段。
我错过了什么?
答案 0 :(得分:0)
您可以使用let div = document.createElement("div");
div.innerHTML = data;
document.getElementsByTagName("body")[0].appendChild(div);
来获取字符串(而不是SimpleXML
):
$response["response"]["response_desc"]
输出:
$xml = simplexml_load_string($response);
echo (string)$xml->response->response_desc;