jQuery ajax回调json解析问题 [英] jQuery ajax callback json parsing issue

查看:108
本文介绍了jQuery ajax回调json解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Github API V3

I'm working with Github API V3

我正在使用以下代码进行ajax调用

I'm using following code to make ajax call

$.ajax({
    type:'POST',
    url: 'https://api.github.com/gists',
    data: JSON.stringify({
        "public": true,
        "files": {
            "sample.html": {
                "content": 'html content'
            }           
        }, 
    }),
    success:function(response){
        alert(response.id);
    }
});

我必须对数据进行字符串化,因为Github API返回错误400!如果我不这样做.在上面的示例中,Github API确实按照我的期望进行了响应.

I have to stringify data as Github API returns Error 400! if i don't. With above example, Github API does response as I expect.

我在回调解析方面遇到了问题.上面的代码适用于webkit&歌剧,但Firefox失败,成功功能.我必须修改以下代码才能在firefox中工作.

I'm having issue with callback parsing though. Above code works with webkit & opera but firefox fails with success function. I have to modify code as below to get work in firefox.

success:function(response){
    alert(JSON.parse(response).id);
}

然后是Webkit& Opera无法使用上述修改后的代码成功响应.

But then Webkit & Opera fails with success response with above modified code.

在所有浏览器中获取成功回调的正确方法是什么?我在做什么错了?

What is correct way to get success callback across all browser ? What I'm doing wrong ?

推荐答案

dataType: 'json',属性添加到您的ajax调用中,以检查其是否起作用.

Add dataType: 'json', property to your ajax call check if it works..like the one below

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

这篇关于jQuery ajax回调json解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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