简体   繁体   English

jQuery AJAX无法成功调用函数

[英]JQuery AJAX doesn't get to success call back function

Here is my jQuery Ajax code 这是我的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()
      });

When my button is clicked it enters to [complete] and [error] call back methods but NOT to "success" call back method. 单击我的按钮时,它会进入[完成]和[错误]回调方法,但不会进入“成功”回调方法。 How can I see what the error is? 如何查看错误是什么?

I tried to curl the same statement and I get a valid response: 我试图卷曲相同的语句,但得到有效的响应:

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

I'm using jQuery 1.7.2 我正在使用jQuery 1.7.2

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

In FireBug it shows that request with 200 OK, but no response body. 在FireBug中,该请求显示为200 OK,但没有响应正文。 But I see response body when I do it via curl. 但是当我通过卷发时看到的是反应体。

What could be the issue? 可能是什么问题? Thanks 谢谢

I guess, it is URL encode problem. 我想这是URL编码问题。 Replace comma , with encode %2C 替换逗号,与编码%2C

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

Cancel the click event 取消点击事件

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

and let jQuery do the proper encoding of the URL 并让jQuery对URL进行正确的编码

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

and hopefully the service and your browser both support CORS since this is a Same Origin Policy issue. 并希望该服务和您的浏览器都支持CORS,因为这是一个同源策略问题。

Did you notice there are no Allow-Access-Origin in Api call? 您是否注意到Api呼叫中没有允许访问源?

I got this on chrome 我在铬上得到了这个

XMLHttpRequest cannot load http://api.openweathermap.org/data/2.5/weather?q=London,uk . XMLHttpRequest无法加载http://api.openweathermap.org/data/2.5/weather?q=London,uk No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://jquery.com ' is therefore not allowed access. 因此,不允许访问源“ http://jquery.com ”。

I think you have a cross site scripting issue. 我认为您遇到跨站点脚本问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM