XMLHttpRequest (unable to read JSON to JavaScript)

Here is the sites.txt which is in JSON which I am trying to parse in JavaScript with XMLHttpRequest but somehow I end up with the plain html page.

[ { "url":"https://www.google.com.pk/", "display":"Google" }, { "url":"http://www.bing.com/", "display":"Bing" }, { "url":"https://github.com/", "display":"Github" } ]

and here is my complete HTML with JavaScript to read the sites.txt file and show the links to the html page.

<div id="sites"></div> <script> var xmlhttp = new XMLHttpRequest(); var url="sites.txt"; xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ myArr = JSON.parse(xmlhttp.responseText); myFunction(myArr); } } xmlhttp.open("GET", url, true); xmlhttp.send(); function myFunction(arr){ var out = ""; var i; for(i = 0; i < arr.length; i++){ out += '<a href="' + arr[i].url + '">"' + arr[i].display + '"</a>'; } document.getElementById("sites").innerHTML = out; } </script>

Category:javascript Views:0 Time:2018-10-08

Related post

  • Application crash when JSONValue unable to read json iphone 2011-02-17

    My Application crash when JSONValue unable to read json iphone? here what i m getting Response [{"profile_pic":"1.jpg","user_name":"Jack"}] 2011-02-17 13:37:38.209 App[3964:207] -JSONValue failed. Error trace is: ( "Error Domain=org.brautaset.JSON.Er

  • XmlHttpRequest - possible to read JSON from other domain? 2012-02-09

    I have found the following piece of code which uses XmlHttpRequest to read JSON data from another domain. It works without using JSONP. I thought it is not possible (http://en.wikipedia.org/wiki/Same_origin_policy) . Why does it work? <script type

  • Unable to read json file and assign value to local varibales 2014-06-28

    I am trying to read json file using php code listed below but I am unable to assign these values to local variables. Can someone help me explain what am I doing wrong? <?php $file = "http://localsurch.com/deals2.txt"; $response = json_decode(file_

  • Reading JSON in Javascript 2012-03-18

    I am trying to parse some json that my server replies me. I am getting this answer from the server: { "ROWCOUNT": 1, "COLUMNS": [ "REGISTRATION_DT", "USERNAME", "PASSWORD", "FNAME", "LNAME", "EMAIL", "MOBILE", "FACEBOOK_ID" ], "DATA": { "REGISTRATION

  • jquery unable to read json response (using $.post) 2011-07-18

    I am submitting a post request with data type json. I am able to see json response in fiddrel but jquery is not able to parse that. Here is my code: $("#jsonTestCasePost").click(function(){ var requestType = $("#requestType").val(); $('#result').val(

  • Reading JSON with JQuery : Field Names 2011-06-22

    I have a json payload like below; { "ABW": "Aruba", "AFG": "Afghanistan", "AGO": "Angola" } As you see, it will have all countries. I can read a JSON payload as it is mentioned here; http://webhole.net/2009/11/28/how-to-read-json-with-javascript/ One

  • read JSON to PHP from javascript 2011-05-16

    I have an API that is available only in javascript (no PHP). It fetches some data of which I made a JSON string. What I need to know is how do I read this string in other page? I tried using following : $json = file_get_contents($url); but of course

  • Help reading JSON from HttpContext.InputStream 2010-08-12

    I have created a HttpModule to capture requests for auditing purposes. For Ajax requests to a web method I would like to also log the JSON data associated with the request. E.g Request POST /MyPage.aspx/AddRecord HTTP/1.1 x-requested-with: XMLHttpReq

  • Parsin JSON Using Javascript and using the result as AJAX 2011-05-07

    Hey guys, right now I'm trying to make a music discovery website just to learn more about web design and programming. It has surely been a challenge, but with help of stackoverflow I'm getting there. Anyways..... I'm using itunes' API to return the a

  • Is there a way to get this (URL inside) JQuery counter to read JSON every 20seconds 2012-02-14

    Currently im using PrivatePub(faye) in a rails app for chat but so far every post i read it looks like faye is unable to provide a count of open sockets subscribed to a specific event(or chat room) So unless anyone here can give me a better idea of h

  • Find value in Json by javascript 2009-11-09

    I cant find one way to get this value ("comment") into json using javascript. var myJSONObject = { "topicos": [{ "comment": { "commentable_type": "Topico", "updated_at": "2009-06-21T18:30:31Z", "body": "Claro, Fernando! Eu acho isso um extremo desres

  • Does jQuery support reading JSON from X-JSON HTTP headers? 2009-11-29

    Is jQuery able to read JSON data from X-JSON HTTP headers returned by the server? I've been searching through the jQuery docs, but all the examples I can find use JSON returned in the request body rather than the headers. --------------Solutions-----

  • Export Visual Studio Template error: "Unable to read an exported file" 2010-03-09

    When exporting a Visual Studio project to a Template (File/Export Template...) I get the following error upon clicking the Finish button: Unable to read an exported file for the following reason: The file cannot be opened with the selected editor. Pl

  • How to read json response as name value pairs in JQuery 2010-10-04

    I want to read json response as name and value pairs in my JQuery code. Here is my sample JSON response that I return from my java code: String jsonResponse = "{"name1":"value1", "name2:value2"}; in my JQuery, if I write jsonResponse.name1, I will ge

  • Reading JSON feed of external HTTPS site 2010-10-09

    Is it possible to read JSON feed available in another site which is HTTPS? I'm getting empty string in return. --------------Solutions------------- It is possible as long as you respect the same origin policy. For cross domain AJAX you could use JSON

  • how to post a json from javascript? 2010-10-29

    i want to send a json string from a html webpage using javascript to a WCF.. is there any good tutorial for that? this is the code i am using <head> <title>Test</title> <script type="text/javascript" src="http://ajax.googleapis.c

  • How to create/clone a JSON in javascript/jquery? 2010-11-08

    I wanna know 2 things first if possible: How to create a JSON object in javascript/jquery? How do I clone a JSON object in javascript/jquery? Hope you guys give me a light :) --------------Solutions------------- How to create a JSON object in javascr

  • Read JSON with ASP.NET 2.0 VB.NET? 2011-01-13

    How can I read JSON with ASP.NET 2.0? Unfortunately we are unable to upgrade the application because of backward compatibility issues. --------------Solutions------------- JSON.NET is a good library on codeplex. It's very easy to use and although the

  • How can I pretty-print JSON using JavaScript? 2011-01-26

    How can I display JSON in an easy-to-read (for human readers) format? I'm looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc. --------------Solutions------------- pretty-printing is implemented natively in

  • Trouble with JQuery Ajax reading JSON 2011-07-10

    I'm using JQuery to try to read JSON and I keep ending up in the error handler. When I inspect the DOM all I see is "error" under statusText with no more details. I've tried saving this to a local file and have added the beforeSend section to address

  • Unable to show JSON data into Grid Panel. Shows only one blank row in Grid 2011-09-21

    Unable to show JSON data into Grid. I got blank grid but you can see sequence no "1" and blank row, though number 1 is auto generated, it is not a JSON data. code Ext.onReady(function(){ // PRESSURE GRID - PRESSURE TAB // var proxy=new Ext.data.HttpP

  • Sencha Touch : unable to parse JSON tree 2011-10-22

    I've just started learning Sencha Touch. Trying to build a simple news reader app. But the app is unable to parse the JSON provided. Can someone please help me find the fault in this structure? [{ 'category': 'int', 'image': 'app/Views/images/img1.jp

  • How to Read JSON Post Object from PHP 2012-03-29

    I have a problem with "JSON, JQuery, Ajax, JavaScript and PHP". I have code to save data. This code send data(JSON) via JQuery Ajax to proses.php. The code for save data shown below : $("#save").click(function(){ var id_promotion_code = $("#id_promot

  • Dojo reading JSON data without an Identifier 2012-04-17

    I'm trying to parse a JSON from a rest service. This service does not put the data into the format that I think ItemFileReadStore wants, but I cannot change it. Everything I have found in the dojo library for reading JSON data requires an identifier,

  • How can i read json with comment with Json.NET 2012-04-25

    In order to install an external extension into Google Chrome browser, I try to update chrome external extension json file. Using Json.NET it seems to be easy: string fileName = "..."; // path to chrome external extension json file string externalExte

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.198 (s). 11 q(s)