Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a web api with basic Get, Put, Post and Delete methods on a localhost web server. the url is "http://localhost:5000/api/todoitem".
The Get method returns to the browsers the following response (Todo Items) :
[{"id":1,"name":"walk dog","isComplete":true},{"id":2,"name":"groceries","isComplete":false},{"id":3,"name":"laundry","isComplete":false},{"id":4,"name":"lssundry","isComplete":false}]


When I attempt an ajax request with the following syntax, The returned data / result is null, and I do not get any error messages.
I would greatly appreciate some help in resolving the problem.
Ajax request looks like this:
JavaScript
$.ajax({
        url: "http://localhost:5000/api/todoitem?callback=processIt",
        dataType: "jsonp",
        type: "POST",
        jsonpCallback: processIt, 
        contentType: "application/json; charset=utf-8",           
        success: function (result, status, xhr) {
            alert("In success function");
           // console.log(result + " : result");
           // console.log(result.contentType + " : result");
           // console.log(status + " : Status");
           // console.log(xhr + " : xhr");
            //processIt(result);                  
        },
        error: function (xhr, status, error) {
            console.log("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
        }
    });
function processIt(r) {
   alert("Process it");
   alert(r);
   alert(r.data);        
}

Thank you very much,
Chieko

What I have tried:

Tried plain JSON and received CORS exception.
Posted
Updated 15-Sep-20 6:34am
v2
Comments
Richard Deeming 16-Sep-20 4:35am    
How have you enabled JSONP support on your API? As far as I can see, there's no built-in support.

It also seems to be deprecated in favour of adding proper CORS headers.
Cross-Origin Resource Sharing (CORS) - HTTP | MDN[^]
ckuroda2001@yahoo.com 17-Sep-20 15:15pm    
Hi Richard,
Thank you for taking the time to read my question.
I have added to the root web . config file, the custom headers tag,
<customheaders>
<add name="Access-Control-Allow-Origin" value="*">

And, originally, I was trying to call the get method from a backbone.js file, but the browser kept throwing the CORS exception. So, I began trying to overcome the CORS exception with a plain Javascript ajax call. So, the returned header is blank

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900