ThisClark ThisClark - 11 months ago 40
Javascript Question

ajax error results in success function call

I was working toward a solution for this recent post: Repeating a function using array of values and in doing so, I stitched together the following piece of code.

<script src=""></script>

var name_list = ['mike','steve','sean','roger'];

var successAction = function(name) {

name_list.forEach(function(name) {
type: "GET",
url: "",
dataType: 'html',
success: successAction(name)


I run this and not surprisingly the following error message is returned:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (Reason: CORS header 'Access-Control-Allow-Origin' missing).

My question is this - If the ajax request results in four failures such as it appears, then why is the success function called four times and correspondingly logging each name in the array?

success: successAction(name) 

could be replaced with

xxx: successAction(name)

and it would still print out the 4 times. The correct syntax should be

success: function(name) { successAction(name); }