Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    227
    Thanks
    26
    Thanked 1 Time in 1 Post

    jquery How to get rid of quotations marks after ajax call ?

    I'm trying to display some plain text from php to a div after an ajax call.

    In my php I am Json encoding, like this:
    Code:
    	echo json_encode('hello');
    And I'm using this Jquery to display that php text to a div:
    Code:
            jQuery.ajax({
                type: "POST",
                url: "reply.php",
                data: postData,
                success: function(response) {
                    jQuery('#result').empty().append(response);
                }
            });
    My div:
    Code:
    <div id="result"></div>
    This works perfectly, as hello will be displayed in my div, however there are Quotation marks around it, so it says "hello" instead of hello.

    I dunno where these quotation marks come from, but I really want to get rid of them, what am I doing wrong ?

  2. #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,619
    Thanks
    58
    Thanked 662 Times in 657 Posts
    Why are you encoding it if it's a simple string? Can't you just echo the string?

  3. #3
    Regular Coder
    Join Date
    Feb 2007
    Posts
    227
    Thanks
    26
    Thanked 1 Time in 1 Post
    I'm using that line for testing only.. I want to display database records with an ajax call, the only problem are those quotation marks :S
    Last edited by docock; 01-10-2017 at 12:31 PM.

  4. #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,619
    Thanks
    58
    Thanked 662 Times in 657 Posts
    If you were echoing real JSON from php you would use JSON.parse(response) in your javascript, to parse the response string into an object (basically by losing the surrounding quotes). I guess you could include that in your experiment, should you choose to continue.

  5. #5
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,230
    Thanks
    21
    Thanked 547 Times in 541 Posts
    Quote Originally Posted by docock View Post
    I dunno where these quotation marks come from
    they come from JSON encoding a simple string.

    I want to display database records with an ajax call
    if your returned database records are a non-primitive structure (read: arrays) then you don't have that problem in the first place. hence you should amend your test to use the data structure that you intend to use for that.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •