开发者

Json problem in fetching data

开发者 https://www.devze.com 2023-01-16 05:32 出处:网络
<script> function jsonfunc(){ var data =\"publick=\"+document.getElementById(\"publickeyval\").value+\"&privatek=\"+document.getElementById(\"privatekeyval\").value;
<script>
  function jsonfunc(){
   var data ="publick="+document.getElementById("publickeyval").value+"&privatek="+document.getElementById("privatekeyval").value;

   var url="http://www.remoteapiserver.com/example_api/example_adcpatchaapi.php?"+data;
   alert(url);
  var my_JSON_object = {}; 
var http_request = new XMLHttpRequest();
http_request.open( "GET", url, true );
http_request.onreadystatechange = function () {
  if (http_request.readyState == 4){
      alert(http_request.responseText+"#"); // showing only # //
       my_JSON_object = JSON.parse( http_request.responseText );
  }
};
http_request.send(null);


  }
  </script>

I was as asked my question as per comment i read Json and writing above code in my php page.But still this is giving problem.I开发者_开发问答 am not getting fetched dada from remote server.I am getting only "#" in alert box.


I highly recommend a JavaScript Framework like jQuery for this kind of stuff. It definitely makes this a lot easier. jQuery lets you do cross-domain requests, if you use JSONP (take a look at the docs). The code would look something like this:

$.getJSON(yourUrlGoesHere, function(data) { // ready callback
    // data is an object; no need to parse it manually.
});


Sometimes it't better use some library: JQuery or Moootools: http://mootools.net/docs/core/Request/Request.JSON

Implement this in native JS is difficulty if we want use it in all browsers ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消