【问题标题】:spell check using google.com/tbproxy/spell使用 google.com/tbproxy/spell 进行拼写检查
【发布时间】:2011-11-12 15:10:12
【问题描述】:

我在我的项目中使用谷歌拼写检查并按照博客中的建议进行所有操作,但它似乎对我不起作用。你能看看这个并告诉我我做错了什么吗?

在我的 JavaScript 中:

function makeRequest(parameters, svalue) {  
  console.log("dv-- inside makerequest 1");
  console.log("svalue =", svalue);
  http_request.onreadystatechange = GetResponse;
  http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en', true);    

  data = '<?xml version="1.0" encoding="utf-8" ?>';
  data +='<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>';
  data += svalue;
  data += '</text></spellrequest>';
  console.log("data =", data)
  http_request.send(data);
}

function GetResponse(){
  console.log("dv-- inside GetResponse-1 http_request.readyState =", http_request.readyState)
  if (http_request.readyState == 4) {
console.log("dv-- inside GetResponse-2 http_request.status =", http_request.status)
    if (http_request.status == 200) {
  http_response = http_request.responseText;
  console.log("dv --http_response =", http_response)
    }
    else {
      console.log('There was a problem with the request' + '& http_request.status =' + http_request.status );
    }
  }
}

我的 PHP 代码:spellify.php

$url="http://www.google.com/tbproxy/spell?lang=en&hl=en";
//$data = file_get_contents('php://input');
$data = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hellow  </text></spellrequest>';

$data = urldecode($data);

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch); 

print $contents;
?>

我尝试在我的 php 中对数据进行硬编码,以确保数据正确传递,这不是问题。

Firefox 报错: Error: unclosed token 源文件:http://mysite.com/Project/spellify.php?lang=en 行:5,列:183 Source Code: $data ='?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoreups="0" ignoredigits="0" ignoreallcaps="0">text>hello thsi is graet</text>';

Chrome 没有给出任何错误,但在控制台中显示如下:

dv-- inside makerequest 1
spellify.js:419svalue = hello worlda 
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 1
spellify.js:427data = <?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hello worlda </text></spellrequest>
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 2
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 3
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 4
spellify.js:435dv-- inside GetResponse-2 http_request.status = 200
spellify.js:438dv --http_response = <?php

$url="http://www.google.com/tbproxy/spell?lang=en&hl=en";
// $data = file_get_contents('php://input');
$data = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hello thsi is graet</text></spellrequest>';
$data = urldecode($data);

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
$contents = curl_exec ($ch);
curl_close ($ch); 

print $contents;
?>

请提出可以纠正的建议。

谢谢

【问题讨论】:

    标签: php javascript ruby-on-rails spell-checking


    【解决方案1】:

    这里是示例实现。

    http://amitgandhi.in/2013/01/25/spell-check-utility-using-jquery-and-google-api/

    • 您可以添加单词词典。变量$.SpellChecker.dicWords 是一个 维护这个的javascript数组。您可以使用您的数据库表 用现有的单词填充这个数组。目前这个应用程序不是 连接到数据库,所以如果你添加你的单词,那么单词的生命周期 只会直到页面重新加载。

    • 使用 google.com/tbproxy/spell api 调用(用 PHP 编写)

    【讨论】:

      【解决方案2】:

      您正在使用来自 spellify.com 的代码,感谢原始开发人员的某种功劳。

      1) 您错误地修改了 spellify.php 文件。 2-a) Spellify 基本上准备 http-request 并通过 Javascript src/spellify.js 解析 http-response,所以在 spellify.php 中进行任何更改都是不正确的,而是你应该检查你是否引用了 spellify/src/scriptaculous.js并正确拼写/src/prototype.js。

      2-b) 在相同的 spellify.js 下,您错误地硬编码了以下行;

      http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en', true);
      

      应该是这样的;

      http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en'+parameters, true);
      

      3) 写 spellify.php 的时候,SSL 验证可能没有问题,但现在它不会返回任何内容,除非你忽略 SSL 验证,在 spellify.php 中添加以下行,在 $contents = curl_exec ( $ch);

      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      

      【讨论】:

        猜你喜欢
        • 2013-03-26
        • 2016-01-22
        • 1970-01-01
        • 1970-01-01
        • 2012-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多