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
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Ajax request/response - Get error from error function

    Hi,

    After an Ajax json request/response - Get following error from error function: Error: parsererror with message: SyntaxError: Unexpected token d

    Json is seen to be returned in Chrome network tab.

    $(document).ready(function() {
    $('form').on('submit', function(event) {
    event.preventDefault();

    $.ajax('tour.json', {
    type: 'POST',
    dataType: 'json',
    data: $('form').serialize(),
    success: function(response) {
    var html = '';

    $.each(response, function(index, entry) {
    html += '<ul>';
    html += '<li>Destination' +entry.description+'</li>';
    html += '<li>Price' +entry.price+'</li>';
    html += '<li>Nights' +entry.nights+ '</li>';
    html += '<li>Confirmation' +entry.confirmation+ '</li>';
    html += '</ul>';
    });
    $('.tour').html(html).fadeIn();
    },
    error: function(request, errorType, errorMessage) {
    console.log('Error: ' + errorType + ' with message: ' + errorMessage);
    },
    contentType: 'application/json'
    });
    });
    });

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,696
    Thanks
    25
    Thanked 657 Times in 656 Posts
    Please help me by posting the tour.json file.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Please help me by posting the tour.json file.
    Very sorry for not posting tour.json, thankyou.

    tour.json:

    [
    {
    description: 'Paris, France Tour',
    price: 2499.0,
    nights: 7,
    confirmation: 'ef63kd'
    }
    ]

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,696
    Thanks
    25
    Thanked 657 Times in 656 Posts
    tour.json: should be:
    Code:
    {
    "description": "Paris, France Tour",
    "price": 2499.0,
    "nights": 7,
    "confirmation": "ef63kd"
    }
    No square brackets.

    Here is an easier way (straight from jquery examples) to render that
    Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8" />
    <title>Title of the document</title>
    <style type="text/css">
    .my-new-list{
    	color: red;
    	border: 2px solid black;
    	width: 200px;
    }
    </style>
    </head>
    
    <body>
    <div class="tour"></div>
    
    <script type='text/javascript' src='javascript/jquery.js'></script>
    <script type="text/javascript">
    $.getJSON( "test.json", function( data ) {
    	var items = [];
    	$.each( data, function( key, val ) {
    		items.push( "<li id='" + key + "'>" + val + "</li>" );
    	});
    	
    	$( "<ul/>", {
    		"class": "my-new-list",
    		html: items.join("")
    	}).appendTo( ".tour" );
    });
    </script>
    </body>
    </html>
    I added styling because the example adds a class to the div. I know it's nowhere like yours, but it is easier.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    ab2qik (03-24-2014)

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, i also found out that this was invalid json after checking with: JSONLint - The JSON Validator.

    Once correctly formatted, the response went into page. I like to keep square bracket as in this case i know there will be more objects entered.


  •  

    Posting Permissions

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