简体   繁体   English

JSONP Ajax回调未触发

[英]JSONP Ajax callback not firing

I'm trying to use JSONP to call a webservice, passing in an integer to get two integers out. 我正在尝试使用JSONP调用Web服务,传入一个整数以获取两个整数。 Normally I'd use regular JSON, but I'm calling to a different port number (to a webservice, from a website, but both in the same domain). 通常,我会使用常规的JSON,但会调用不同的端口号(从网站到Web服务,但都在同一域中)。

For context, I'm passing in the user id to get out the number of unread tasks and notices, to populate some figures shown on the screen. 对于上下文,我传入用户ID以获取未读任务和通知的数量,以填充屏幕上显示的一些图形。

Here's the code: 这是代码:

var url = "localhost:13742/api/notices/get" ;
$.getJSON(url + "?callback=?", $("#__UserId").val(), function (response) {
    console.log("Number of unread tasks- " + response.UnreadTasks + ". Number of unread notices- " + response.UnreadNotices);
});

As the title suggests, the callback I've specified isn't being fired- I'm not getting any logs, including errors. 如标题所示,我指定的回调没有被触发-我没有收到任何日志,包括错误。 I know the server-side code is working because I can manually get to it through the browser- I just pass in the query string and it works fine (returns the information as an XML document). 我知道服务器端代码正在工作,因为我可以通过浏览器手动获取它-我只需传递查询字符串,它就可以正常工作(将信息作为XML文档返回)。 There's no security on it for now, so I don't have to worry about passing in authentication headers. 目前尚无安全性,因此我不必担心传递身份验证标头。

This is my first time using JSONP, although I've used regular JSON plenty. 这是我第一次使用JSONP,尽管我经常使用常规JSON。 Maybe there's a simpler option given that I can do it so easily through the browser. 也许有一个更简单的选择,因为我可以通过浏览器轻松地做到这一点。

Your server needs to return a valid JSONP response for this to work. 您的服务器需要返回有效的JSONP响应才能正常工作。 JSONP is special in that it is just a JavaScript file. JSONP的特殊之处在于它只是一个JavaScript文件。 That's how it works cross-domain, because it just adds a <script> tag to your head. 这就是跨域工作的方式,因为它只是在您的头部添加了<script>标记。

JSONP is not JSON, nor is it XML. JSONP 不是 JSON,也不是XML。 You say your URL returns the data as an XML document? 您说您的URL以XML文档形式返回数据吗? That's incorrect. 不对 That's why it's not working. 这就是为什么它不起作用。

JSONP should just be a function call, to the function passed in via callback= , with the data as the 1st parameter. JSONP应该只是对通过callback=传入的函数的函数调用,数据作为第一个参数。

I don't know what language your server is, but in PHP it could look like this: 我不知道您的服务器是哪种语言,但是在PHP中,它看起来可能像这样:

<?php
$data = array(
    'UnreadTasks' => 5,
    'UnreadNotices' => 8
);

header('Content-type: application/javascript');
echo $_GET['callback'] . '(' . json_encode($data) . ');';

Try using ajax like this: 尝试像这样使用ajax

$.ajax({

  url: "localhost:13742/api/notices/get",

  dataType: "jsonp",

  data: $("#__UserId").val(),

  success: function(data) {
    console.log("Number of unread tasks- " + data.UnreadTasks + ". Number of unread notices- " + data.UnreadNotices);
  },

  error: function(jxhr, status, err) {
    console.log("Error, status = " + status + ", err = " + err);
  }
});

Also did you make sure 'localhost:13742/api/notices/get' supports jsonp - http://en.wikipedia.org/wiki/JSONP 您是否还确保'localhost:13742 / api / notices / get'支持jsonp- http://en.wikipedia.org/wiki/JSONP

Turns out it was a client-side issue. 原来这是一个客户端问题。 Apparently the user id needed to be included in the URL. 显然,用户ID需要包含在URL中。 Seems the server was alright after all. 毕竟服务器似乎还不错。 Rather boring solution, but there we go. 相当无聊的解决方案,但是我们走了。

Thanks to everyone that assisted. 感谢大家的帮助。

$.ajax({
            url: "localhost:13742/api/Notices/" + $("#__UserId").val(),
            dataType: "jsonp",
            success: function (data) {
                console.log("Number of unread tasks- " + data.UnreadTasks + ". Number of unread notices- " + data.UnreadNotices);
            },
            error: function (jxhr, status, err) {
                console.log("Error, status = " + status + ", err = " + err);
            }
        });

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

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