jQuery 为什么 Ajax get 请求失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9349284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why Ajax get Request failed
提问by Umesh Patil
The response of my request is a java script code. When I put the url in browser, I can see the whole generated java script code on the page. Format of url passed to $.ajax is as below:
我的请求的响应是一个 java 脚本代码。当我将 url 放入浏览器时,我可以在页面上看到整个生成的 java 脚本代码。传递给 $.ajax 的 url 格式如下:
http://localhost:8080/vi-api/viapi?action=tag&projectId=45&tagId=345
When I put the above URL I can see the request is successful. Now, I am using below Ajax request for this url using jQuery.
当我输入上面的 URL 时,我可以看到请求成功。现在,我使用 jQuery 在这个 url 的下面使用 Ajax 请求。
var finalUrl = "http://localhost:8080/vi-api/viapi?action=tag&projectId=45&tagId=345";
var req = $.ajax({
type:"GET",
url:finalUrl,
type:"script",
data:"",
success: function(html){
alert('Requese sucessful.');
},
complete:function(jqXHR, textStatus) {
alert("request complete "+textStatus);
},
error: function(xhr, textStatus, errorThrown){
alert('request failed->'+textStatus);
}
});
Question 1:This gives the alert "request failed error'. Why this is so ?
问题 1:这给出了警报“请求失败错误”。为什么会这样?
Question 2:Is there any way to return success/failure code in above process?
问题2:以上过程有没有办法返回成功/失败代码?
回答by regilero
In:
在:
$.ajax({
type:"GET",
url:finalUrl,
type:"script",
(...)
You have two times the 'type' key in your object. So I think only the second one is taken ('script'). Obviously 'script' is not a valid HTTP method (as HEAD,GET,PUT,POST, etc). The keyword your were looking at for 'script' is maybe dataType
which may be one of xml
, json
, jsonp
, text
, script
, or html
.
您的对象中有两次“类型”键。所以我认为只有第二个被采用(“脚本”)。显然“脚本”不是有效的 HTTP 方法(如 HEAD、GET、PUT、POST 等)。您正在寻找的 'script' 关键字dataType
可能是xml
, json
, jsonp
, text
, script
, 或 之一html
。
Do not forget to look at jsonp
, it's usually a nice way to return a script content and to call it.
不要忘记查看jsonp
,它通常是返回脚本内容并调用它的好方法。
回答by Sean
I am not sure why, but I can give your some tips how to debug or find out issues:
我不知道为什么,但我可以给你一些关于如何调试或找出问题的提示:
1) install fiddlerto look at HTTP request.
1) 安装fiddler来查看 HTTP 请求。
2) type:"script", why the type is script? try to use "text/html".
2)type:"script",为什么type是script?尝试使用“文本/html”。
3) use complete(jqXHR, textStatus)
you can look at HTTP status. more info about $.ajax
3)使用complete(jqXHR, textStatus)
可以查看HTTP状态。有关 $.ajax 的更多信息
回答by Darin Dimitrov
var finalUrl=http://localhost:8080/vi-api/viapi?action=tag&projectId=45&tagId=345;
is pretty invalid javascript. You probably meant passing the url as a string:
是非常无效的 javascript。您可能意味着将 url 作为字符串传递:
var finalUrl = 'http://localhost:8080/vi-api/viapi?action=tag&projectId=45&tagId=345';