0

是否可以使用 Javascript/Html5 检查特定网页的 http 回复代码?例如,如果用户输入一个句子,我希望检查我是否有句子中每个单词的音频文件。我意识到我应该使用数据库来查找单词的可用性,但我目前正在开发一个非常简单的演示,该演示目前由一个 html 文件和一堆 ogg 文件组成。

4

3 回答 3

1

一段时间以来,XHR 的当前版本实际上已经允许人们通过它们进行表单提交。我已经使用它一段时间来对 MySql 执行 javascript RPC,它的工作原理就像一个冠军。当然,XHR 一如既往地拥有您正在寻找的 HTTP 状态代码。

只需在 Mozilla 站点上查看 XMLHTTP 的当前文档,您就会立即获得所需的内容

于 2013-05-12T20:37:35.543 回答
1

您可以尝试使用 ajax 来实现。这是一个使用 jquery ajax 函数的简单示例:

// Assuming this is the word you are looking for
var str = "bells";

$.ajax( "www.example.com/audio/" + str + ".ogg" ).done(function () {
   // An audio file for that word exists
}).fail( function () {
   // There is no audio file for that word
});

对于整个句子,您可以将其拆分为单词并单独查找每个单词。

于 2013-05-12T20:42:19.280 回答
1

XMLHttpRequest 绝对是要走的路,假设您不打算使用数据库(我强烈推荐,因为它们非常棒。)

function oggSearch () {
  alert(this.responseText);
};

var sInput = ''; //grab it from whatever element you're using
var oggReq = new XMLHttpRequest();
oggReq.onload = oggSearch;
//I'm not sure if concatenating the file type will cause issue
oggReq.open("get", sInput + ".ogg", true);
oggReq.send();

我显然没有测试过上述内容,但它基于下面的链接。

https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest

于 2013-05-13T02:48:48.847 回答