weixin_33743703 2009-08-10 21:20 采纳率: 0%
浏览 37

getJSON失败,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.

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:

FORM 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>

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);
    });         
});

The JSON response:

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

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.

  • 写回答

4条回答 默认 最新

  • weixin_33739523 2009-08-10 21:34
    关注

    Are you able to manually call your service without any errors? Have you tried using firebug and looked under XBR to see the post/response of the JSON payloads? I normally use .NET as my endpoints, and with .NET 3.5 I need to use content type "application/json; charset=utf-8".

    Here is an example of a working JSON call I use in .NET with jQuery 1.3.2

    $.ajax({   
        type: "POST",   
        url: "WebService1.ASMX/HelloWorld",   
        contentType: "application/json; charset=utf-8",   
        dataType: "json",   
        data: "{}",   
        success: function(res) {   
        // Do your work here.   
        // Remember, the results for a ASMX Web Service are wrapped   
        // within the object "d" by default. e.g. {"d" : "Hello World"}   
        }   
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数