我有一个使用Jquery和Ajax提交到PHP脚本的表单。 PHP脚本返回一些XML。由于某种原因,Ajax成功函数没有触发,错误的是。
有人能看到我出错的地方吗?
我的Jquery如下
$('#submit-excuse').submit(function (event) {
event.preventDefault();
ws_url = 'http://jacamo.epiphanydev2.co.uk/content/inc/excuse-submit.php?excuse='+$('input#excuse').val();
$.ajax({
type: 'GET',
url: ws_url,
dataType: "xml",
beforeSend: function() {
$('p#response').text('Sending.');
},
success: function(xmlIn) {
results = xmlIn.getElementsByTagName("ReportID");
},
error: function() {
$('p#response').text('Error.');
}
});
});
我的PHP脚本如下:
$excuse = $_GET['excuse'];
$badwords = array (
'one',
'two',
'three',
'four',
'five'
);
if ($excuse == '') {
$error = 'enter something';
} else {
foreach ($badwords as $word) {
$pos = strpos($excuse, $word);
if($pos !== false) {
$passed = false;
}
}
if ($passed !== false) {
$username = 'xxxxx';
$password = 'xxxxx';
$message = $excuse;
$url = 'http://twitter.com/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
$passed = 'yes';
}
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "\t<result>\n";
echo "\t\t<passed>" . $passed . "</passed>\n";
echo "\t</result>";
}
由于
答案 0 :(得分:1)
Jquery期待从您的查询返回xml响应。您需要发送xml标头。尝试在xml header ("content-type: text/xml");
之前添加并查看是否有效
答案 1 :(得分:0)
AJAX不能在不同的域中使用,为此,请查看JSON。
这就是我使用它的方式:
JQuery方面:
var server = ‘http://www.yoursite.com/path/to/map/’;
var name = ‘Peter’;
var lastname = ‘Marcoen’;
$.getJSON(server+’getData.php?callback=?’,{name:name,lastname:lastname}, function(data) {
alert(data); //Do your thing
});
PHP方:
$name = $_REQUEST['name'];
$lastname = $_REQUEST['lastname'];
$return = ‘Hello ‘ . $name . ‘ ‘ . $lastname;
echo $_GET['callback'] . ‘(’ . json_encode($return) . ‘)’;