无法弄清楚为什么$ .ajax调用中的php脚本失败 [英] Can't figure out why php script failing on $.ajax call

查看:99
本文介绍了无法弄清楚为什么$ .ajax调用中的php脚本失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的jQuery脚本:

Here's my jQuery script:

$.ajax({
type: 'POST',
url: 'zipCodes.php',
data: searchZipCode,
cache: false,
success: function(response) {
    alert ('response from db search: ' + response);
},
error: function(response) {
    console.log(response);
    alert('Query failed, php script returned this response: ' + response);
}
});

上面的脚本位于表单所在的html文件的head标记中.php脚本与html文件位于同一文件夹中.

The above script is in the head tag of the html file the form is in. The php script is in the same folder as the html file.

这是表格:

<form id="frmZipCodeSearch" action="">
    <label for="txtZipCodeSearch">Enter a zip code</label>
    <input type="text" id="txtZipCodeSearch" length="10" maxlength="5" />
    <input type="submit" id="btnSubmitZipCodeSearch" />
</form>

我还验证了"searchZipCode"具有值.

I also have verified that "searchZipCode" has a value.

每次运行搜索时,我都会从ajax调用的错误部分得到警报,该消息说

Every time I run the search I get the alert from the error part of the ajax call, which says

查询失败,php脚本返回以下响应:[object Object]

Query failed, php script returned this response: [object Object]

,这在控制台中显示:

对象{readyState:0,setRequestHeader:函数,getAllResponseHeaders:函数,getResponseHeader:函数,overrideMimeType:函数……}

Object {readyState: 0, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}

我尝试将php脚本剥离为一个回显.我没有在服务器上使用脚本在文件夹中生成任何php错误日志.我所做的任何改变似乎都不会给我带来不同的结果.除了一个例外:

I've tried stripping the php script down to just an echo. I'm not getting any php error logs generated in the folder with the script on the server. Nothing I change seems to give me a different result. With one exception:

我唯一得到不同结果的是,当我剥离ajax并将php脚本移动到具有表单的文件中并执行常规" html表单POST方法时.那行得通.但是我不认为,当php是另一个文件时,路径是错误的,因为在控制台中找不到找不到文件".

The only time I got a different result was when I stripped out the ajax and moved the php script to the file with the form and did a "regular" html form POST method. That worked. But I don't think the path is wrong when the php is a different file because I'm not getting a "file not found" in the console.

我在这里想念什么?

[其他信息] 为了回应hek2mgl的回答,我在错误响应中添加了xhr并在控制台中获得了更多信息:

[Additional Info] In response to hek2mgl's answer, I added xhr to the error response and did get some more info in the console:

Uncaught ReferenceError: xhr is not defined     zipCodeSearch.html:57
    $.ajax.error                                zipCodeSearch.html:57
    l                                           jquery-1.8.3.min.js:2
    c.fireWith                                  jquery-1.8.3.min.js:2
    T                                           jquery-1.8.3.min.js:2
    r                                           jquery-1.8.3.min.js:2

我正在使用的所有其他jQuery都可以正常工作,但这是我第一次亲自使用v1.8.3.

All other jQuery I'm using is working fine, but this is the first time I've used v1.8.3 myself.

[答案-信用hek2mgl] 正如hek2mgl在我们疯狂的讨论中所解释的,我提交了两次表单,因为ajax调用位于表单按钮的click事件之内.

[Answer - credit hek2mgl] As hek2mgl explained in our crazy-long discussion, I was submitting the form twice, because the ajax call was within the click event of the form's button.

此问题的解决方法是将ajax调用从click事件移到其自己的函数中,并在我的客户端验证之后,调用ajax函数并添加"return false;".在函数调用之后.

The fix for this was to move the ajax call out of the click event into its own function, and after my client-side validation, call the ajax function and add "return false;" after the function call.

推荐答案

$.ajax错误回调的定义如下:

The $.ajax error callback is defined as follows:

类型:函数(jqXHR jqXHR,字符串textStatus,字符串errorThrown)

Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

您已经转储了整个jqXHR对象.与成功函数不同,重发文本本身不是错误回调的参数.要检索响应文本(如果有),请使用以下命令:

You've dumped the whole jqXHR object. Unlike the success function, the respone text itself is not a param of the error callback. To retrieve the response text (if any) use this:

error: function(xhr) {
    var response = xhr.responseText;
    console.log(response);
    var statusMessage = xhr.status + ' ' + xhr.statusText;
    var message  = 'Query failed, php script returned this status: ';
    var message = message + statusMessage + ' response: ' + response;
    alert(message);
}

这篇关于无法弄清楚为什么$ .ajax调用中的php脚本失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆