Ajax error causes success function call

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?

+5
source share
1 answer
 success: successAction(name) 

can be replaced by

 xxx: successAction(name) 

and it will still print 4 times. The correct syntax should be

 success: function(name) { successAction(name); } 
+2
source

Source: https://habr.com/ru/post/1238454/


All Articles