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 8948531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:01:01+00:00 2026-06-15T13:01:01+00:00

I have a php script in which i have the following function : <?php

  • 0

I have a php script in which i have the following function :

<?php
function readXML() {
    $url = $_REQUEST['schemaPath'];

    $xml = simplexml_load_file($url);

    $fields = $xml -> fields -> field;
    GLOBAL $array;
    GLOBAL $c;
    $array = new stdClass;
    foreach($fields as $field->attributes){
        foreach($field->attributes->attributes() as $a => $b){
            if($a == "name") {
                $c = $b;
            }
            if($a == "type") {
                $array -> $c = $b;
            }
        }
    }
    return json_encode($array);
}
echo readXML();
?> 

I am making ajax call in the following way :

$.ajax({
                cache: false,
                url: "readXML.php",
                type: "POST",
                dataType: 'jsonp',
                jsonp: 'jsonp_callback',
                data: { schemaPath: "http://localhost:8983/solr/admin/file/?file=schema.xml" },
                crossDomain: true,
                success:function(data) {
                    if (!data) {
                        alert("Error in processing xml file");
                        return null;
                    } else {                
                        console.log(data);
                    }
                },
                error:function(data) {
                    alert("Error while reading schema file.");
                    $("#loadingStatus").hide();
                }
            });

I am not getting the desired json response format. I get the alert Error while reading schema file in the response. I actually want it be as a key:value pattern like as $c:$b but it is coming like $c:{"0":$b}. How to return the array from the php script such that i can have a valid json response.

  • 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-06-15T13:01:02+00:00Added an answer on June 15, 2026 at 1:01 pm

    I got the solution why on completion of ajax call it was always going to the error function. Here i am doing jsonp in the ajax call but was not handling the same in the php script. To solve this problem and to return a proper response need to add the following in the php script :

    if(isset($_REQUEST['jsonp'])) {
            echo $_REQUEST['jsonp'].'('.json_encode($array).')';
        }else{  
            echo json_encode($array);
        }
    

    And to make the response as key:value need to change the $array[$c] = $b; to $array[$c] = (string)$b; along with the change pointed by @EmmanuelG in his answer.

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

Sidebar

Related Questions

I have the following function in a PHP script, which returns and API call:
I have a PHP script using Doctrine 2 which does essentially the following: $entityManager->transactional(function($em)
I have a php script (generator.php) which generates certain HTML/XML/SVG depending on various conditions.
I have my front end script which has the following jQuery code: $.get(/backend/post.php, {
I have a typical mail.php script, which uses the mail() function. After the user
I have the following script which works fine locally: <?php //Report all errors error_reporting(E_ALL);
I have a PHP script that loads a function which uses Curl to log
I have script which has the following structure: <?php if( a == b ){
I have a php script which uses the following line to submit a form
I have the following php script which works flawlessly in normal circumstances (i.e. visiting

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.