我在我的项目中使用google拼写检查,并按照博客中的建议进行了所有操作,但它似乎对我不起作用。能否请您看一下,并告诉我我做错了什么。
在我的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,列:183Source 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;
?>
请提出可以更正此工作的建议。
谢谢
最佳答案
您正在使用来自spellify.com的代码,对原始开发人员的某种贡献值得赞赏。
1)您错误地修改了spellify.php文件。
2-a)Spellify基本上是通过Javascript src / spellify.js准备http-request并解析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验证,否则现在它不会返回任何内容,请在$ contents = curl_exec($ ch)之前在spellify.php中添加以下几行;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);