的getJSON失败,JSON只会验证 [英] getJSON fails, JSON validates

查看:140
本文介绍了的getJSON失败,JSON只会验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的getJSON调用它莫名其妙地失败了。我们的想法是,你点击提交注释,URL被击中它决定是否注释是确定或中有淘气的话。响应给出了JSON格式。

I have a getJSON call which is inexplicably failing. The idea is, you click to submit a comment, a URL gets hit which determines if the comment is OK or has naughty words in it. The response is given in JSON form.

下面是成对下来JS生成呼叫。注释和URL已经在页面上,它抓住他们,并点击的网址:

Here's the paired down JS that generates the call. The comment and the URL are already on the page, it grabs them and hits the URL:

HTML表单:

<fieldset id="mg_comment_fieldset" class="inlineLabels">
<div class="ctrlHolder">
    <textarea id="id_comment" rows="10" cols="40" name="comment"></textarea>
</div>
<div class="form_block">
    <input type="hidden" name="next" value="" />
    <input id="mg_comment_url" type="hidden" name="comment_url" value="" />

    <input id="mg_comment_submit" type="submit" value="Remark" />
</div>
</fieldset>

具体JS BLOCK发送/ READS响应:

SPECIFIC JS BLOCK THAT SENDS/READS RESPONSE:

$('input#mg_comment_submit').click(function(){
var comment = $("textarea#id_comment").val();
var comment_url = $('input#mg_comment_url').val();
$.getJSON(
    comment_url+"?callback=?&comment="+comment+"&next=",
    function(data){
        console.log(data);
        alert(data);
    });         
});

JSON响应:

The JSON response:

[{"errors": {"comment": ["Weve detected that your submission contains words which violate our Terms and Conditions. Please remove them and resubmit test"]}}]

它是被返回的应用程序/ JSON一个媒体类型。这将验证JSONLint。我也尝试添加一对夫妇AJAX功能,试图发现错误,而且他们都沉默了。我可以看到请求走出去的萤火虫,和回来的状态200答复,进一步验证在JSONLint和我可以在响应的JSON标签穿越就好了。如果我把警报在之前的getJSON,它运行;它只是在它里面运行的,没有什么。我还发现,如果我改变.getJSON到不用彷徨,警报不运行,这表明它的东西与JSON。我的想法是什么问题可以。使用Firefox 3.0.13。

It's being returned as a mimetype of application/json. It validates in JSONLint. I also tried adding a couple AJAX functions to try to catch errors, and they're both silent. I can see the request going out in Firebug, and coming back as status 200 responses, which validate in JSONLint and which I can traverse just fine in the JSON tab of the response. If I put an alert before the getJSON, it runs; it's just that nothing inside of it runs. I also find that if I change .getJSON to .get, the alerts do run, suggesting it's something with the JSON. I'm out of ideas as to what the problem could be. Using Firefox 3.0.13.

推荐答案

查询参数回调=?进场时,如果你使用的是跨站点脚本或JSONP,如果你张贴在同一台服务器,你并不需要使用。

The querystring parameter "callback=?" comes into play if you are using cross-site scripting or jsonp, if you are posting the same server, you don't need to use that.

如果您需要或希望使用该选项,在服务器端code需要回来与包含在JSON响应的回调函数。

If you need or want to use that option, the server side code needs to come back with the callback function included in the json response.

示例:

$jsonData = getDataAsJson($_GET['symbol']);
echo $_GET['callback'] . '(' . $jsonData . ');';
// prints: jsonp1232617941775({"symbol" : "IBM", "price" : "91.42"});

因此​​,要么做一个服务器端的变化,如果有必要或简单删除回调=?从URL参数。

下面是对更多信息JSONP

这篇关于的getJSON失败,JSON只会验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆