简体   繁体   English

如何使用身份验证从URL解析javascript的JSON

[英]How to parse JSON with javascript from url with authentication

Im trying to parse some JSON from a URL. 我试图从URL解析一些JSON。 It is password protected and I cannot figure it out. 它受密码保护,我无法弄清楚。 Tried many tutorials and many blogs but nothing worked so far. 尝试了许多教程和博客,但到目前为止没有任何效果。

Ive got set up this basic code, just to see if it returns anything. 我已经建立了这个基本代码,只是看它是否返回任何东西。 Parsing will be easier. 解析会更容易。 But it seems im not even getting data from the url. 但似乎我什至没有从URL获取数据。

I just need it to send me something back and display it in html. 我只需要它可以发回一些东西并以html显示。

<pre>
var base_url = "https://backend.sigfox.com/api/devices/166A4/messages";

function example()
{
    var response = "";
    var form_data = {
        username: "57616e7551e24e4c99acf6f3",
        password: "fd7e78536e4e834e9ce4fba0bf747947"
    };
    $.ajax({
        type: "POST", 
        url: base_url, 
        data: form_data,
        success: function(response)
        {
            alert(response);

        var json_obj = $.parseJSON(response);//parse JSON

            var output="<ul>";
            for (var i in json_obj) 
            {
                output+="<li>" + json_obj[i].data+ "</li>";
            }
            output+="</ul>";

            $('span').html(output);
        },
        dataType: "json"//set to JSON    
    })    
}
</pre>

Thanks for your help 谢谢你的帮助

Test your code every step of the way to make sure it's doing what you want. 测试代码的每一步,以确保它正在执行您想要的操作。 First, make sure your password is working. 首先,请确保您的密码有效。 Once you've tested that, then print out the raw JSON data and copy and paste it into a parser like 测试完之后,请打印出原始JSON数据,然后将其复制并粘贴到解析器中,例如

https://jsonformatter.curiousconcept.com/ or http://jsonformatter.org/ https://jsonformatter.curiousconcept.com/http://jsonformatter.org/

make sure the JSON is valid 确保JSON有效

After you have tested everything, Then feed it into the methods you are using to parse the JSON. 测试完所有内容之后,然后将其输入到您用来解析JSON的方法中。

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

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