简体   繁体   中英

getting error in $.ajax call

I have create a web API in C# and call that api from jquery but getting error:

('#buttonid').click(function (event) {
    $.ajax({
     method: "GET",url: "http://localhost:54580/api/persons",
     dataType: 'jsonp',
      beforeSend: function (request) {
                request.setRequestHeader("Authorization", "Negotiate");
            },
       async: true,
     success: function (data) {

     console.log(JSON.stringify(data));

    },
      error: function(jqXHR,error, errorThrown) {  
           if(jqXHR.status&&jqXHR.status==400){
                 console.log(jqXHR.responseText); 
           }else{
                console.log("Something went wrong");
           }
      }
}); 
});

in debug mode I am able to see my data.:

http://localhost:54580/api/persons?callback=jQuery111103874004708615564_1461475734608&_=1461475734609

The data is:

[{
    "FirstName": "Ramu",
    "LastName": "Rai",
    "Email": "test123"
}, {
    "FirstName": "Ramu1",
    "LastName": "Rai1",
    "Email": "test123"
}]

but code never goes in success section. it always print "Something went wrong"

Use json instead of jsonp , try demo below (replace url)

 $.ajax({ method: "GET", url: "http://jsonplaceholder.typicode.com/posts/1", //"http://localhost:54580/api/persons", beforeSend: function(request) { request.setRequestHeader("Authorization", "Negotiate"); }, success: function(data) { var result = JSON.stringify(data); var $result = document.querySelector('#pre'); $result.textContent = result; }, error: function(jqXHR, error, errorThrown) { if (jqXHR.status && jqXHR.status == 400) { console.log(jqXHR.responseText); } else { console.log("Something went wrong"); } } }); function my_function(data){ console.log('my_function', data); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre id="pre"></pre> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM