why Ajax get Request failed

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

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:

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.

   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 ?

Question 2:Is there any way to return success/failure code in above process?

回答1:

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.

Do not forget to look at jsonp, it's usually a nice way to return a script content and to call it.



回答2:

I am not sure why, but I can give your some tips how to debug or find out issues:

1) install fiddler to look at HTTP request.

2) type:"script", why the type is script? try to use "text/html".

3) use complete(jqXHR, textStatus) you can look at HTTP status. more info about $.ajax



回答3:

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:

var finalUrl = 'http://localhost:8080/vi-api/viapi?action=tag&projectId=45&tagId=345'; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!