简体   繁体   English

JSONP可以在jQuery中运行,但不能在mootools中运行

[英]JSONP works in jQuery but not in mootools

I'm trying to convert my code to Mootools (I like the coding paradigm better). 我正在尝试将我的代码转换为Mootools(我更喜欢编码范例)。

I am doing cross-domain AJAX where I own both domains (closed network). 我正在进行跨域AJAX,我拥有这两个域(封闭网络)。 I am just requesting simple JSON from my server. 我只是从我的服务器请求简单的JSON。 I get these errors in Mootools (jQuery works): 我在Mootools中遇到这些错误(jQuery工作):

Resource interpreted as Script but transferred with MIME type text/plain. Uncaught SyntaxError: Unexpected token :

var url = http://localhost:3000/

Server: 服务器:

var http = require('http'),
    json = {"hi" : false};

http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(JSON.stringify(json));
}).listen(3000, function() {
    console.log("Server on " + 3000);
});

jQuery: jQuery的:

$.ajax({
    url: url,
    type: "GET",
    dataType: 'json',
    success: function (data) {
    }
});

Mootools: MooTools的:

var myRequest = new Request.JSONP({
    url: url,
    onComplete: function (data) {
        alert(JSON.stringify(data));
    }
});
myRequest.send();

I've tried adding these headers to no avail.: 我尝试添加这些标题无济于事:

'Accept': 'application/json',
'Content-Type': 'application/json'

This seems to be a client-side thing and not a server-side thing since it works in jQuery. 这似乎是一个客户端的东西,而不是服务器端的东西,因为它在jQuery中工作。

What does URL look like? URL是什么样的? jQuery figures out it's a JSONP request by adding ?callback= or ?foo= to the url. jQuery通过向url添加?callback=?foo=来确定它是一个JSONP请求。 Request.JSONP instead uses an option callbackKey . Request.JSONP使用选项callbackKey

There's no method option for JSONP (in any library), since it's just injecting a script tag. JSONP(在任何库中)没有method选项,因为它只是注入脚本标记。

var myRequest = new Request.JSONP({
  url: url,
  callbackKey: 'callback'
  onComplete: function(data){}
}).send();

I have a feeling, however, you aren't using JSONP, but rather XHR with JSON. 我有一种感觉,但是,你没有使用JSONP,而是使用带有JSON的XHR。 If that's the case, use Request.JSON , not Request.JSONP . 如果是这种情况,请使用Request.JSON ,而不是Request.JSONP


Edit 编辑

Since it sounds, from the comments on this answer, that you're not using JSONP, just do this: 因为从这个答案的评论来看,你没有使用JSONP,只需这样做:

new Request.JSON({
  url: url,
  method: 'get',
  onSuccess: function (data){
    console.log(data)
  }
}).send()

Edit 2 编辑2

To change the request headers just add them as an option: 要更改请求标头,只需将它们添加为选项:

new Request.JSON({
  headers: {
    'X-Requested-With': 'XMLHttpRequest',
    'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
  },
  url: url,
  method: 'get',
  onSuccess: function (data){
    console.log(data)
  }
}).send()

You had a syntax error in your code. 您的代码中存在语法错误。

var myRequest = new Request.JSONP({
    url: url,
    method: 'get',
    onComplete: function(data){}
});
myRequest.send();

Also, the response should be wrapped with callback function, example: http://jsfiddle.net/zalun/yVbYQ/ 此外,响应应该包含回调函数,例如: http//jsfiddle.net/zalun/yVbYQ/

Your ajax call should be like this 你的ajax调用应该是这样的

          $.ajax({
                type: "GET",
                url: "http://localhost:17370/SampleService.asmx/HelloWorld",
                data: "{}",
                crossDomain: true,
                contentType: "application/json; charset=utf-8",
                dataType: "jsonp",
               async:false,
                success:function(data,status){
                    alert(data);

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("Error Occured!" + " | " +
                    XMLHttpRequest + " | " + textStatus + " | " +
                    errorThrown);
                }
            });

Your server side method should not return simple string it should return response in jsonp format for that you need to follow this blog: 你的服务器端方法不应该返回简单的字符串,它应该以jsonp格式返回响应,因为你需要关注这个博客:

http://msrtechnical.blogspot.in/ http://msrtechnical.blogspot.in/

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

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