简体   繁体   English

在ajax调用中没有得到json响应

[英]Not getting json response in ajax call

<html>
  <head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script>
    function fn(){
      $.ajax({
        type:'GET',
        url: "http://www.enquiry.indianrail.gov.in/ntes/NTES",
        data: "action=getTrainForDate&trainNo=16649&trainStartDate=11/04/2014&t=1397216860215&18q1xp3lm5=1ptur1oxbz1i5vwea0u61397214250740",
        dataType: "json",
        success:function(data){
          alert(data);
        }
      });
    }   
</script>
  </head>
  <body>
    <a href="#" onclick="fn();"> hi </a>
  </body>
</html>

您请求的 URL 返回(function(){location.reload();})() ,它不是 JSON、JSONP 或任何形式的有用数据。

Just to update you need to use jsonp instead of json because you're going to get CORS error otherwise.只是为了更新你需要使用jsonp而不是json因为否则你会得到 CORS 错误。 Updated code is更新的代码是

$.ajax({
    type:'GET',
    url: "http://www.enquiry.indianrail.gov.in/ntes/NTES",
    data: {"action" : "getTrainForDate", "trainNo" : "16649", "trainStartDate" : "11/04/2014", "t" : "1397216860215", "18q1xp3lm5" : "1ptur1oxbz1i5vwea0u61397214250740"},
    dataType: "jsonp",
    success:function(data){
        alert(data);
    }
});

Note there's nothing wrong in your way of sending data as string, I just prefer object way.请注意,您将数据作为字符串发送的方式没有任何问题,我只是更喜欢对象方式。 Now when you make a call you'll end up getting this message (and not data)现在,当您拨打电话时,您最终会收到此消息(而不是数据)

Resource interpreted as Script but transferred with MIME type text/plain: "http://www.enquiry.indianrail.gov.in/ntes/NTES?callback=jQuery2119973546624…1397216860215&18q1xp3lm5=1ptur1oxbz1i5vwea0u61397214250740&_=1397220999300".

This is where problem is.这就是问题所在。 If you hit the formed URL, it'll give you JavaScript function as a response and no data.如果你点击形成的 URL,它会给你 JavaScript 函数作为响应,而不是数据。 This is what you get as a response这是你得到的回应

(function(){location.reload();})()

I think, server expects unique token (identified by "18q1xp3lm5" : "1ptur1oxbz1i5vwea0u61397214250740" set), for each request.我认为,对于每个请求,服务器都需要唯一的令牌(由"18q1xp3lm5" : "1ptur1oxbz1i5vwea0u61397214250740"标识"18q1xp3lm5" : "1ptur1oxbz1i5vwea0u61397214250740"集)。 If condition is not met, it sends reload request to the browser.如果不满足条件,则向浏览器发送重新加载请求。

Since the call is made through AJAX, it's not able reload the page, but due to reused token, there's no result.由于调用是通过 AJAX 进行的,因此无法重新加载页面,但由于重用令牌,因此没有结果。

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

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