JQuery AJAX没有't获得成功回调功能

JQuery AJAX doesn't get to success call back function

本文关键字:成功 回调 功能 AJAX 没有 JQuery      更新时间:2023-09-26

这是我的jQuery Ajax代码

$(document).ready(function() {
          $("#submitService").click(function(){  
              alert("Before ajax");
            $.ajax({
               type: "GET",
               url: "http://api.openweathermap.org/data/2.5/weather?q=London,uk",
               headers: {"accept": "application/json"},
             success: function (dataItem) {
                alert("Success..");
            },
             complete: function (dataItem) {
                alert("Completed");
            },
              error: function (dataItem) {
                 alert("Error in making engine call."+dataItem);
              }
             });
             alert("After ajax");
            });                 //click()
      });

当我的按钮被点击时,它进入[完成]和[错误]回调方法,但不是"成功"回调方法。我如何才能看到错误是什么?

我试图歪曲同样的陈述,得到了有效的回应:

curl "http://api.openweathermap.org/data/2.5/weather?q=London,uk" -H "accept: application/json"

我使用的是jQuery 1.7.2

<script type="text/javascript" src='<spring:url value="/resources/jquery/js/jquery-1.7.2.min.js"/>'></script>

在FireBug中,它显示了200 OK的请求,但没有响应体。但当我通过卷曲来做时,我看到了反应体。

可能是什么问题?感谢

我想,这是URL编码问题。用编码%2C 替换逗号,

url: "http://api.openweathermap.org/data/2.5/weather?q=London%2Cuk"

取消点击事件

$("#submitService").click(function(e){  
    e.preventDeault();
    ...

让jQuery对URL 进行正确的编码

type: "GET",
url: "http://api.openweathermap.org/data/2.5/weather",
data: {q : "London,uk" },

希望该服务和您的浏览器都支持CORS,因为这是一个同源策略问题。

您注意到Api调用中没有Allow Access Origin吗?

我在chrome 上得到了这个

XMLHttpRequest无法加载http://api.openweathermap.org/data/2.5/weather?q=London,英国。不请求的上存在"Access Control Allow Origin"标头资源原点'http://jquery.com因此不允许访问。

我认为您有一个跨站点脚本问题。

将application/json更改为application/x-www-form-urlencoded