How to send to another domain using ajax

I work on a website (a so-called website) that wants to save some custom data.

Thus, we realized that the best way is to create another site (website) that could handle this. Thus, when a user visits website A, he calls up the website and passes some information through.

The code I use (which I got from this post How to get json from MVC4 C # without javascript and without Ajax )

using (var client = new HttpClient())
{
    var responseString = client.GetStringAsync("http://www.example.com/recepticle.aspx?withQueryString=true"); //I don't actually use the responseString 
}

The fact is that when I asked this question, a comment was left (now deleted), explaining that I should use WebApi. So what I did ... And here everything goes wrong.

I created a Web Api 2 project and I submit it using Ajax and it works on my localhost. I now deployed in my test environment and realized that I could not achieve what I want because of problems with the cross domain.

Further reading suggests that I can not use json, but should be used jsonp , but , jsonpworks only with getwhere I need post( how to use the type: "POST" in jsonp ajax-call ).

I think I could use get and just ignore the answer, but it seems like a hack ...

Sorry for asking two questions, but I think they are very related.

1: jsonp , get, get (- B)? , A - B jsonp , - B, -B ( , B -A)?

2 ( ): A B javascript/ajax/jquery. CORS -, ajax json?

+4
2

, :

http://encosia.com/using-cors-to-access-asp-net-services-across-domains/

, webconfig .

<system.webServer>
 <httpProtocol>
  <customHeaders>
   <add name="Access-Control-Allow-Origin" value="*" />
   <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
 </httpProtocol>
</system.webServer>
+1

-, , .

WebsiteA = App = Web-Client application
WebsiteB = API = Data provider 

1:

jsonp , get, get (- B)? , - - B, jsonp , - - B, - ( , B -A)?

1:

API JavaScript ( ) . JSONP - () , . . . , , , .

" ", <script> JavaScript src=. JSON, <script> , var weatherData = { ... }, weatherData . , . , .

App.html

( )

<html>
<head>
<title> Weather App
<script src=weater-station.com/next-week/?callback=parseRemoteWeatherData />
<script>
var app = {
   main: function() { /* init views, etc */ },
   receiveWeatherData: funciton(maybeJson) {
      // validate payload in maybeJson
   }
}

// expose the callback
window.parseRemoteWeatherData = app.receiveWeatherData

API /next -week/? callback = parseRemoteWeatherData​​h3 >

HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 00:23:53 GMT
Server: Apache/2.0.61 
Access-Control-Allow-Origin: *
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/javascript

parseRemoteWeatherData('any-text, but JSON by convention');

, :

script, .


2:

A B javascript/ajax/jquery. CORS -, ajax json?

2

CORS ( HTTP 1.1) POST , :

  • Content-Type application/x-www-form-urlencoded, multipart/form-data text/plain.

Content-Type: application/json, CORS , . OPTION ( ) Access-Control-Allow-Origin: * (or the domain name of the client), Origin: hostname.tld.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/Server-Side_Access_Control https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest http://www.w3.org/TR/cors/ https://en.wikipedia.org/wiki/JSONP

+1

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


All Articles