Tinypaste API替换字符

时间:2018-12-06 16:50:31

标签: javascript jquery ajax

我需要有关TinyPaste API的帮助。 我做到了:

var profiles = {
    firstProfile: {
        name: "DemostanisYt",
        avatar: "https://i.imgur.com/p3kb3ie.png"
    }
}
profiles = JSON.stringify(profiles)
var response

$.ajax({
    url: "http://www.penyacom.org/api/v1/paste.php",
    type: "POST",
    data: {
        code: JSON.stringify(profiles)
    },
    success: function(e) {
        response = JSON.parse(e)
        $.ajax({
            url: "http://" + response.raw_link,
            type: "GET",
            crossOrigin: "anonymous",
            success: function(e) {
                console.log(e)
            }
        })
    }
})

一个问题:它对代码做了一些处理。而不是像这样:

{"firstProfile":{"name":"DemostanisYt","avatar":"https://i.imgur.com/p3kb3ie.png"}}

它是这样做的:

"{\"firstProfile\":{\"name\":\"DemostanisYt\",\"avatar\":\"https://i.imgur.com/p3kb3ie.png\"}}"

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您正在双重配置文件。声明配置文件时,应该可以删除JSON.stringify函数,或者在ajax请求中发送数据时也可以删除它:

var profiles = {
    firstProfile: {
        name: "DemostanisYt",
        avatar: "https://i.imgur.com/p3kb3ie.png"
    }
}
profiles = JSON.stringify(profiles) // <--- You're stringifying the object here
var response

$.ajax({
    url: "http://www.penyacom.org/api/v1/paste.php",
    type: "POST",
    data: {
        code: JSON.stringify(profiles) // <--- Then restringifying it again here
    },
    success: function(e) {
        response = JSON.parse(e)
        $.ajax({
            url: "http://" + response.raw_link,
            type: "GET",
            crossOrigin: "anonymous",
            success: function(e) {
                console.log(e)
            }
        })
    }
})