Javascript 我可以得到jQuery';s加载(url)以从响应中向我提供成功/错误反馈?
我基本上使用的插件使用jQueryJavascript 我可以得到jQuery';s加载(url)以从响应中向我提供成功/错误反馈?,javascript,jquery,Javascript,Jquery,我基本上使用的插件使用jQueryload(url)函数 由于.load()允许您使用带有响应的回调函数作为参数,因此我想知道是否有方法在.load()调用的文件中嵌入一个指示错误或成功的变量 在调用的文件中,您可以使用类似于$.myPlugin.response=“出错”的内容,回调函数将能够读取它(至少在firefox中是这样)。但是问题是,$.myPlugin.response现在是全局的,如果我们同时创建两个.load(),我们将无法判断哪个调用出错 是否有方法将此错误变量包含在回调函数
load(url)
函数
由于.load()允许您使用带有响应的回调函数作为参数,因此我想知道是否有方法在.load()调用的文件中嵌入一个指示错误或成功的变量
在调用的文件中,您可以使用类似于$.myPlugin.response=“出错”
的内容,回调函数将能够读取它(至少在firefox中是这样)。但是问题是,$.myPlugin.response
现在是全局的,如果我们同时创建两个.load(),我们将无法判断哪个调用出错
是否有方法将此错误变量包含在回调函数中,或者至少包含在比全局函数更窄的范围内
一种方法是使用正则表达式解析响应并查找特定的表达式,但这并不是正确的方法
自定义标题的使用能以某种方式解决问题吗
我希望这个问题有意义
干杯当Ajax调用以错误响应结束时,您可以告诉jQuery调用函数(请参阅): 当然,必须返回HttpServerError才能获得此结果(如何执行此操作取决于服务器端框架) 例如,使用Django:
from django.http import HttpResponse, HttpResponseServerError
def myAjaxCall(request):
""" Return the result if present;
if not present, return an error with the string "No value to return"
in case of any other error, return the error with its text.
"""
try:
value = compute_my_value(request)
if not value:
raise Exception, 'No value to return!'
return HttpResponse(value, mimetype="application/json")
except Exception, e:
return HttpResponseServerError(unicode(e))
让我们从下面的一个例子开始:
正文{字体大小:12px;字体系列:Arial;}
成功响应(应为空):
错误响应:
$(“#success”).load(“/not-here.php”),函数(响应、状态、xhr){
如果(状态=“错误”){
var msg=“抱歉,出现错误:”;
$(“#error”).html(msg+xhr.status+“”+xhr.statusText);
}
});
现在,我们将修改脚本以处理web服务器或CGI脚本返回的自定义错误:
<script>
$("#success").load("/not-here.php", function(response, status, xhr) {
if (status == "custom_error") {
// the handler for custom_error goes here
}
});
</script>
$(“#success”).load(“/not-here.php”),函数(响应、状态、xhr){
如果(状态==“自定义错误”){
//自定义_错误的处理程序位于此处
}
});
你读过这本书了吗?看看“complete”回调函数的签名。是的,我有,我猜你指的是“complete(responseText,textStatus,XMLHttpRequest)一个回调函数,在请求完成时执行。”我找不到最后两个参数的解释。如果您能提供更多帮助,我们将不胜感激。textStatus
(请参阅“complete(jqXHR,textStatus)”)XMLHttpRequest
是XMLHttpRequest
对象,它是所有浏览器中所有axax请求的基础(注意,从jQuery 1.5开始,您会得到一个非常类似的jqXHR对象)。我想我不得不从服务器端发送这些头。这是没有问题的事实,我不知道什么标题需要设置。如果我改为这样问:我需要发送什么标题才能从textStatus和XMLHttpRequest中获得要使用的内容。我可以设置一个自定义头,在完整(responseText、textStatus、XMLHttpRequest)回调中请求它吗?这样的标题会是什么样子?你为什么暗示我没有读过?这个例子对我有什么帮助?它没有说任何关于发送自定义错误的内容。只是处理一个没有成功的请求。或者,如果是的话,我显然从我问起就不明白了。。。谢谢你的帮助对不起,也许我误解了你的问题。。。假设请求的web服务器和页面在您的控制下,您可以发回所需的任何错误$加载(“#success”).load(“/not here.php”,函数(response,status,xhr){if(status==“your#error”){//执行与您的#error$(“#error”).html(msg+xhr.status+”+xhr.statusText)});非常感谢马斯克拉特先生。这正是我需要的帮助。现在,我将尝试找出需要设置服务器端以获得我要查找的结果的标题。不客气。我将编辑我的响应,以使将来的读者更清楚。接下来的问题是:如何让我的后端将响应标记为错误?你可以说我懒惰,但我更愿意从知道答案的人那里得到帮助,而不是阅读大量的文本。错误在调用本身失败时使用。正如我所写的,这不是我所需要的。@Roatin Marth-你也不是很有帮助。
<!DOCTYPE html>
<html>
<head>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
<b>Successful Response (should be blank):</b>
<div id="success"></div>
<b>Error Response:</b>
<div id="error"></div>
<script>
$("#success").load("/not-here.php", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
</script>
</body>
</html>
<script>
$("#success").load("/not-here.php", function(response, status, xhr) {
if (status == "custom_error") {
// the handler for custom_error goes here
}
});
</script>