The parsing error parsing function is not called jquery ajax error?

I try to use all the methods that I saw in the stack overflow set by other users before. But none of them work. Please hope that any of you will point me in the right direction.

$.ajax({
 type: "get",
 dataType:'jsonp',
 params:jsonData,
 jsonp:false,
 jsonpCallback:"callbackfn",
 headers: { "api_key": "u5FocU4xLq2rBfZ1ZSV8o81R2usYzUEM3NaCinnV"},
  url: "http://localhost/url?name=xxx&email=xxxxxx@gmail.com",
 success:function(){
  alert("sucess function");
   },
  error: function(jqXHR, textStatus, errorThrown){
       alert(textStatus + " and<br> " + errorThrown);
    }
  });
 function callbackfn(data) {
   alert(data);
  }

answer { "firstName":"John", "lastName":"Doe" }


Although the answer is json, this results in an error

An analysis error .callbackfn is not called.

+4
source share
2 answers

To use a custom callback function with JSONP, you must declare its scope global, i.e.

window.callbackfn = function(data) {
    alert(data);
};

Why?

, JSONP JavaScript , JavaScript, <script>. script , script, script, ​​ . , JSONP .


OP , : JSONP AWS Lambda/API Gateway . JSONP .

+1

, . , JSON, JSONP . :

callbackfn({
  "firstName":"John",
  "lastName":"Doe"
});

jsonp: false jsonpCallback dataType: 'jsonp' - , jsonp. , JSONP - -, jsonpCallback :

$.ajax({
   type: "get",
   dataType:'json',
   params:jsonData,
   headers: { "api_key": "u5FocU4xLq2rBfZ1ZSV8o81R2usYzUEM3NaCinnV"},
   url: "http://localhost/url?name=xxx&email=xxxxxx@gmail.com",
   success:function(data){
     callbackfn(data);
   },
   error: function(jqXHR, textStatus, errorThrown){
       alert(textStatus + " and<br> " + errorThrown);
    }
});
0

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


All Articles