dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Ajax request/response - Get error from error function

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Location
    Berlin, Germany
    Posts
    8

    Ajax request/response - Get error from error function

    To all that could give their feedback. An Ajax json request is made with a form submit handler that triggers it.

    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.

    HTML FORM ----

    <div class="tour" data-daily-price="357">
    <h2>Paris, France Tour</h2>
    <p>
    $<span id="total">2,499</span> for <span id="nights-count">7</span> Nights
    </p>

    <form action="tour.json" method="POST">
    <p>
    <label for="nights">Number of Nights</label>
    </p>
    <p>
    <input type="number" name="nights" id="nights" value="7">
    </p>
    <input type="submit" value="book">
    </form>
    </div>


    JS ------

    $(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'
    });
    });
    });
    Last edited by ab2qik; 03-22-2014 at 07:38 AM.

  2. #2
    Join Date
    Apr 2011
    Location
    Berlin, Germany
    Posts
    8
    Very sorry for not posting tour.json, thankyou.

    tour.json:

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

  3. #3
    Join Date
    Apr 2011
    Location
    Berlin, Germany
    Posts
    8
    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.


    Correct format - tour.json:

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

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,504
    Please "Wrap" code in code tags.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles