Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 956855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:36:22+00:00 2026-05-16T00:36:22+00:00

HI, I am using the following code request.js var request; function runAjax(JSONstring) { //

  • 0

HI,

I am using the following code

request.js

var request;
function runAjax(JSONstring)
{

    // function returns "AJAX" object, depending on web browser
    // this is not native JS function!
    request = new XMLHttpRequest();

    request.open("GET", "request.php?json="+JSONstring, true);
    request.onreadystatechange = sendData;
    alert('called');
    request.send(null);
}

function sendData()
{
    // if request object received response
    if(request.readyState == 4)
    {
    // parser.php response
    var JSONtext = request.responseText;
    // convert received string to JavaScript object
  try
  {
  //var JSONobject = eval('(' + JSONtext + ')');
    var JSONobject = JSON.parse(JSONtext);
  }
  catch(e)
  {
    var err="Error: "+e.description;
    alert(err);
  }
 alert('1');
    // notice how variables are used
  try {

    var msg = "Number of errors: "+JSONobject.errorsNum+        "\n- "+JSONobject.error[0]+     "\n- "+JSONobject.error[1];

    alert(msg);
  }
  catch(ee)
  {
    var errr="Error: "+ee.description;
    alert(errr);
  }
    }
}

The php function I have used here is request.php

<?php

// decode JSON string to PHP object
$decoded = json_decode($_GET['json']);

// do something with data here
 echo "Decoded string - ". $decoded;
// create response object
$json = array();
$json['errorsNum'] = 2;
$json['error'] = array();
$json['error'][] = 'Wrong email!';
$json['error'][] = 'Wrong hobby!';

// encode array $json to JSON string
$encoded = json_encode($json);


// send response back to index.html
// and end script execution
die($encoded);

?>

I’m calling this JavaScript function from a HTML page request.html

<html>
<head>
<script src="request.js">
</script>
</head>
<body>
<a href="Javascript:runAjax('vinoth')">call</a><br>
</body>
</html>

The problem here is I was getting Syntax Error at the line:24

var JSONobject = JSON.parse(JSONtext);

if I was using

var JSONobject = eval('(' + JSONtext + ')');

I was getting ” ) Expected Error “

After that I deleted browser cache. Restarted the browser, now the code seems working very fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:36:22+00:00Added an answer on May 16, 2026 at 12:36 am

    You should verify JSON format, then look up there – http://ru.wikipedia.org/wiki/JSON. You can get an idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following bit of code: function Node(){ .... function foo(request){ for (var name
i am using the following code to send request to the server var url='getDNormalForm.action';
Doing an ajax get request works as expected using the following code: $.ajax({ type:
I am using following code to show a spinning wheel: $(#loading) .hide() .ajaxStart(function(){ $(this).show();
I am using following code to get bitmap from url. This function is used
I've got the following code: <script type=text/javascript> $(function(){ $(#AddMaps).submit(function(){ var $form = $('#AddMaps'); $.ajax({
i am fetching data using following code _.each(cardIds, function(id){ var mdc = new MdlCard({cardId:
Given the following code var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type':
I have the following snippet of code: $('input#teamName').live('blur', function() { var value = $(this).val();
I'm using the following code to send a request: var ajaxHandler = new XMLHttpRequest();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.