I was working on a solution for this recent post: Repeating a function using an array of values , and while doing this, I sewed the following code snippet.
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script> var name_list = ['mike','steve','sean','roger']; var successAction = function(name) { console.log(name); } name_list.forEach(function(name) { jQuery.ajax({ type: "GET", url: "https://www.google.com/", dataType: 'html', success: successAction(name) }); }); </script>
I run this, and it is not surprising that the following error message is returned:
Cross-request request is blocked: a policy of the same origin prohibits reading a remote resource at https://www.google.com/. (Reason: CORS header "Access-Control-Allow-Origin" is missing).
My question is this . If the ajax request leads to four errors, such as it appears, then why is the success function called four times and, accordingly, registers each name in the array?
source share