Click here to Skip to main content
15,885,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i acan use ajax and api action in one project and send data and update data correctly.
but when i want to send data from ajax in my project to api action in my another project,the api action dont work!

$.ajax({
                  type: "post",
                  url: urll, error: function () { alert("errore") },
                  data: {id: 4},
                  success: function (dataa) {
                      alert("ok");
                      var x = JSON.stringify(dataa, null, '\t');
                      var dataaa = $.parseJSON(x); alert(dataaa.price);
                      dataa.price -= 1.02;
                      var url2 = 'https://localhost:44337/api/apiialbums/PutAlbum';
                      //var url2 = 'http://localhost:51111/api/AlbumsApi/PutAlbum';
                      $.ajax({
                          type: "PUt",
                           //async: false,
                          url: url2,
                          //data: JSON.stringify(dataa, null, '\t'),
                          data: {album: dataa },
                          //contentType: "application/json; charset=utf-8", ///////
                          //dataType:"json",
                          success: function (dataaz) {
                              alert("updated");
                              alert(dataaz.price);
                              var x2 = JSON.stringify(dataaz, null, '\t');
                              alert(x2);
                          },
                          error: function () {
                              alert("error in update..");
                          }
                      })
                  }
              });

and my api [httpput]action:
[HttpPut]
      public async Task<ActionResult<Album>> PutAlbum([FromBody]Album album)
      {
          _context.Entry(album).State = EntityState.Modified;
          await _context.SaveChangesAsync(); return album;
      }


when i breakpoint in api action,
I understand that the program does not refer to the action

What I have tried:

I searched the internet a lot but I could not find the reason, please help me to solve this problem as soon as possible
Posted
Updated 20-Sep-20 1:30am

1 solution

For cross projects (assuming you are trying to make an AJAX requests to another domain), you need to have CORS enabled.
Refere: Enable Cross-Origin Requests (CORS) in ASP.NET Core | Microsoft Docs[^]

There are three ways to enable CORS:

1. In middleware using a named policy or default policy.
2. Using endpoint routing.
3. With the [EnableCors] attribute.

Though in case your another project is part of the same domain just the end page change, make sure the url's are correct, up and running. (I see you are trying with localhost:port)
 
Share this answer
 

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