Я посылаю Ajax запрос на URL-адрес и получить следующий ответ:Анализировать JSON и HTML данные ответа Ajax
Ajax запрос:
<div id="htmldata"></div>
<script type="text/javascript">
jQuery.ajax({
type: "GET",
url: "http://testing.local/index.php",
dataType: "html",
success: function(response) {
// Parse response here ...
}
});
</script>
Ответ:
<div class="dataset">
<h1 class="title">List of Data</h1>
<table width="100%" align="center" class="datatable" >
<tr>
<td class="dataField" ><label>Data 1</label></td>
<td class="dataValue">Value 1</td>
</tr>
<tr>
<td class="dataField" ><label>Data 2</label></td>
<td class="dataValue">Value 2</td>
</tr>
<tr>
<td class="dataField" ><label>Data 3</label></td>
<td class="dataValue">Value 3</td>
</tr>
</table>
</div>
{"status":"success", "message":"Received data successfully"}
В ответе ajax есть оба типа данных: json и html.
Поэтому я хочу сообщить сообщение об успешном или неудачном сообщении из json-данных и установить html-код в div с id «htmldata» с помощью jQuery или javascript.
Эта строка json всегда находится на последней строке вашего ответа? –
Да, это всегда в последней строке, но данные json могут быть разными. –
var responsetext = response.split ("\ n"); alert (responsetext [responsetext.length -1]); Попробуйте это Или вы можете использовать javascripts последний индекс. –