Javascript 为什么Ajax发送对象作为响应?
我有一个非常简单的php脚本,它可以向ajax发送请求并返回我放入.php文件中的字符串,但是当请求响应时,它会发送一个对象而不是字符串。我不知道为什么会这样,因为我以前已经用同样的方法做过了,效果很好 这是发送请求的表单Javascript 为什么Ajax发送对象作为响应?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我有一个非常简单的php脚本,它可以向ajax发送请求并返回我放入.php文件中的字符串,但是当请求响应时,它会发送一个对象而不是字符串。我不知道为什么会这样,因为我以前已经用同样的方法做过了,效果很好 这是发送请求的表单 <form method="POST" id="personForm"> <div class="form-group col-md-6"> <label for="NameInput">Name</label
<form method="POST" id="personForm">
<div class="form-group col-md-6">
<label for="NameInput">Name</label>
<input type="text" name="name" class="form-control" id="NameInput">
</div>
<div class="form-group col-md-6">
<label for="lNameInput">Last Name</label>
<input type="text" name="lastname" class="form-control" id="lNameInput">
</div>
<input type="button" name="Send" class="btn btn-info" onclick="ajaxRequest($('#NameInput').val(), $('#lNameInput').val())" value="Send">
</form>
<hr>
<div id="result">
</div>
我不知道为什么我在ajax的响应中没有得到回音的字符串。而是发送一个对象。我正试图用这个数据库创建其他项目,但我有同样的问题。请参阅。您正在使用complete
回调,它接收jqXHR
对象作为其第一个参数
相反,如果要使用返回的数据,您希望使用success
(两个c
s,注意),而不是complete
success
接收数据作为其第一个参数。(您也可以使用complete
删除正在进行的消息等。)
例如:
function ajaxRequest(name, lastn) {
var params = {
"name" : name,
"lastn" : lastn
};
$.ajax({
url: './process/resquestAjax.php',
method: 'POST',
data: params,
beforeSend: function() {
$('#result').html('<p>Procesando Peticion...</p>');
},
complete: function(completeResult) {
// If you wanted to do something whether the request
// succeeded or failed, you'd do it here. Otherwise,
// remove this handler.
},
success: function(successResult) {
$('#result').html(successResult);
},
error: function(jqXHR,estado,error){
alert('There was an error!: '+estado+' name-> '+error+' otro-> '+jqXHR);
alert("Please contact support ias soon as posible...!");
}
}); // End Ajax Call
}
函数ajaxRequest(名称,lastn){
变量参数={
“姓名”:姓名,
“lastn”:lastn
};
$.ajax({
url:“./process/resquestAjax.php”,
方法:“POST”,
数据:params,
beforeSend:function(){
$(“#结果”).html(“Procesando Peticion…”);
},
完成:函数(completeResult){
//如果你想做某事,无论请求
//成功或失败,你都会在这里做,否则,
//删除此处理程序。
},
成功:函数(成功结果){
$('#result').html(successResult);
},
错误:函数(jqXHR、estado、error){
警报('出现错误!:'+estado+'name->'+error+'otro->'+jqXHR);
警报(“请尽快联系支持ias…”;
}
});//结束Ajax调用
}
您想使用success
(而不是success
)根据结果执行操作。如果您向AJAX调用添加dataType:text,
,它应该会修复它。@JayBlanchard-如果返回了正确的头,应该没有任何必要。(如果他们不是,他们应该是::-)我同意@t.J.Crowder-但有时你只是不知道OP会做什么:)
$nombre = $_POST['name'];
$apellido = $_POST['lastname'];
echo "¡Hello! your name is : ". $nombre ." and your last name: ". $apellido;
function ajaxRequest(name, lastn) {
var params = {
"name" : name,
"lastn" : lastn
};
$.ajax({
url: './process/resquestAjax.php',
method: 'POST',
data: params,
beforeSend: function() {
$('#result').html('<p>Procesando Peticion...</p>');
},
complete: function(completeResult) {
// If you wanted to do something whether the request
// succeeded or failed, you'd do it here. Otherwise,
// remove this handler.
},
success: function(successResult) {
$('#result').html(successResult);
},
error: function(jqXHR,estado,error){
alert('There was an error!: '+estado+' name-> '+error+' otro-> '+jqXHR);
alert("Please contact support ias soon as posible...!");
}
}); // End Ajax Call
}