/    Sign up×
Community /Pin to ProfileBookmark

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’
});
});
});

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ab2qikauthorMar 24.2014 — Very sorry for not posting tour.json, thankyou.

tour.json:

[

{

description: 'Paris, France Tour',

price: 2499.0,

nights: 7,

confirmation: 'ef63kd'

}

]
Copy linkTweet thisAlerts:
@ab2qikauthorMar 24.2014 — 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"

}

]
Copy linkTweet thisAlerts:
@rootMar 24.2014 — Please "Wrap" code in code tags.
×

Success!

Help @ab2qik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...