简体   繁体   English

SyntaxError:missing; 在声明jquery jsonp之前

[英]SyntaxError: missing ; before statement jquery jsonp

I am using below code to access rest service hosted on another domain. 我使用下面的代码访问托管在另一个域上的休息服务。

$.ajax({
            type: 'GET',
            url: url,
            async: false,
            jsonpCallback: 'jsonCallback',
            contentType: "application/json",
            dataType:"jsonp",
            success: function(json) {
                alert(json);
            },
            error: function(e) {
               console.log(e.message);
            }
        });

I am able to get the data correctly, but I get this error in firebug in mozilla: 我能够正确获取数据,但是我在mozilla中的firebug中得到了这个错误:

SyntaxError: missing ; SyntaxError:missing; before statement 在声明之前

{"Hello":"World"} {“你好,世界”}

Can anyone suggest me what I am doing wrong here? 谁能告诉我我在这里做错了什么? Even though Json data is valid. 即使Json数据有效。 I tried all the suggestions posted in this question But still I am getting same error. 我尝试了这个问题中发布的所有建议但是我仍然得到同样的错误。

If it's really JSON you ask for, don't set "jsonp" as dataType , and don't provide a callback : 如果它是你要求的JSON,请不要将"jsonp"设置为dataType ,并且不提供回调:

$.ajax({
        type: 'GET',
        url: url,
        contentType: "application/json",
        success: function(json) {
            alert(json);
        },
        error: function(e) {
           console.log(e.message);
        }
});

the format of JSON and JSONP are slightæy different JKSONP is a function invocation expression JSON和JSONP的格式略有不同JKSONP是一个函数调用表达式

callback({"hellow":"world"});

whereas JSON is simply a serialized object 而JSON只是一个序列化对象

{"Hello":"world"}

fromyour posting it would seem that the server is returning JSON and not JSONP 从你发布它似乎服务器返回JSON而不是JSONP

So you either need to change the server to reply correctly (The actual callback name is a get parameter to the request). 因此,您需要更改服务器以正确回复(实际的回调名称是请求的get参数)。 You should do this if you are using the ajax call across domains 如果您跨域使用ajax调用,则应该执行此操作

If you are not using ajax across domains stick to regular JSON 如果你没有跨域使用ajax坚持常规JSON

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

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