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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:10:40+00:00 2026-05-31T04:10:40+00:00

Suppose I have a PHP script that returns a json response. It looks something

  • 0

Suppose I have a PHP script that returns a json response. It looks something like this:

$ctype = ContentNego::desiredContentType();
header("Content-Type: $ctype");

if (ContentNego::flavor($ctype) == ContentFlavor::JSON) {
    echo '{ "name": "' . $name . '", ' .
        '"comment": "' . $comment . '"}';
}
.... other content types here....

Prior to the code above, I have

class ContentFlavor {
    const XML = 1;
    const JSON = 2;
    const TEXT = 3;
    const Unknown = 4;
}

And the value of $cftype is set by examining the incoming Accept header, in a basic form of content-type negotiation.

The string echoed by the php script looks like this:

[ "name" : "Vettel", "comment" : "He's terrific" ]

In the browser, I have javascript like this:

    $.ajax({type: "GET",
            url: url,
            dataType: "json",
            headers : { "Accept" : 'application/json' },
            cache: false,
            error: function (xhr, textStatus, errorThrown) {
                enableEditboxButtons();
            },
            success: function (data, textStatus, xhr) {
                // fill and show the editbox dialog
                ...
            }});

But when I receive this content on the jQuery side, I get this:

[ "name" : "Vettel", "comment" : "He\'s terrific" ]

Notice the injected backslash “escaping” the apostrophe or single-quote. My code did not put that backslash there. It seems to have been injected by PHP? Also it seems to be against the requirements of JSON encoding as stipulated on http://json.org:

enter image description here

The resulting json string with the escaped apostrophe fails to parse in jQuery 1.7.1, and my error fn gets called.

What’s the deal? Is PHP really doing this escaping, and if so why/how? (It runs on PHP 5.2.17 on Linux) Can I avoid it?

If not, how can I work around this problem?

  • 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-31T04:10:41+00:00Added an answer on May 31, 2026 at 4:10 am

    Cheeso, make sure the *magic_quotes_gpc* and *magic_quotes_runtime* option is turned off on the PHP configuration (that could be putting the backslash there, more info here).

    Also you probably should use the json_encode method if you can, it makes life a lot easier as you can pass an array or object directly to it and get a JSON version of the represented data.

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

Sidebar

Related Questions

So I have this php script that output an html table with data about
I have a PHP script that's supposed to take in a URL and search
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
I would like run a PHP script that runs every second to update a
In javascript suppose you have this piece of code: <div> <script type=text/javascript>var output =
I have a array which contains values like this See below for script sample
I know that this question might have been asked like 100 times, but, believe
This is a general question. Using an example below, suppose that I have a
I have an application that posts data to a php script through jquery's ajax
Suppose I have a sting as PHP Paddy,PHP Pranav,PHP Parth, now i have a

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.