简体   繁体   English

jQuery AJAX JSONP无效标签

[英]JQuery AJAX JSONP Invalid Label

I know this question has been asked, but I cannot get it working. 我知道有人问过这个问题,但我无法解决它。

I execute the following AJAX request: 我执行以下AJAX请求:

function dislikeMeme(memeId) {
    $.ajax({
        dataType: "jsonp",
        url: "http://<url>.com/dislike/" + memeId,
        data: { 
            u: "username",
            p: "password"
        },
        jsonpCallback: 'successCallback'
    });
}

function successCallback(data) {
    alert("Test"); // Not firing because of previous 'Invalid label' error
};

Looking at firebug I see that the request was successful, but there is an Invalid Label error which fires the Error callback of the request. 查看firebug,我看到请求成功,但是有一个Invalid Label错误触发了请求的Error回调。 The response of the request is as follows: 请求的响应如下:

{
    "id":6220673,
    "myScore":-1,
    "msg":"Not loved"
}

I see that the parentheses are causing JavaScript to interpret the response as an object, but I know this is the format I am retrieving, isn't there anyway to parse this before it causes an error? 我看到括号导致JavaScript将响应解释为一个对象,但是我知道这是我要检索的格式,在引起错误之前,是否有解析它的方法?

I also see that the URL of the page returning this information is: 我还看到返回此信息的页面的URL是:

http://<url>.com/dislike/123456?callback=successCallback&u=username&p=password&_123456789

Everything is working perfectly except this Invalid label error. 除此Invalid label错误外,其他所有东西都正常运行。 Does anyone have any ideas? 有人有什么想法吗?

Thanks in advance everyone 预先感谢大家

A server that can handle JSONP takes the callback paramater (can be different parameter depending on WS) and passes it in the response. 可以处理JSONP的服务器将使用回调参数(取决于WS的不同参数)并将其传递给响应。 So the response from the server should be: 因此,服务器的响应应为:

successCallback({
"id":6220673,
"myScore":-1,
"msg":"Not loved"
})

If you don't have control over the server your only route is proxy. 如果您无法控制服务器,则唯一的途径就是代理。 See my cross domain answer for information on getting around same origin policy. 请参阅我的跨域答案,以获取有关解决同一原产地政策的信息。 What prevents me from using $.ajax to load another domain's html? 是什么使我无法使用$ .ajax加载另一个域的html?

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

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