I work on a website (called websiteA) who wants some bespoke data to be stored.
As such, we realised the best way is to have another website (websiteB) which would handle this. So, when a user visits websiteA, it calls websiteB and passes some information across.
The code I used to use (which I recieved form this post How to get json from MVC4 C# with no javascript and no Ajax) is
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 point is though, when I asked that question, a comment was left (now deleted) explaining I should use WebApi instead. So I did... And this is where it is going wrong.
I have created the Web Api 2 project, and I post using Ajax and it works in my local host. I've now deployed to my test environment and realised I can't achieve what I want because of cross domain origin issues.
Further reading suggests I can't use json
, but must use jsonp
but, jsonp
only works with get
where as I need post
(How to use type: "POST" in jsonp ajax call).
I guess I could use 'get' and just ignore the response but this feels like a hack...
Sorry for asking 2 questions but I think they're very much related.
Question 1 is: if the jsonp requires a callback, is it actually using a get but the get is being called from the target machine (websiteB)? So, instead of posting from websiteA to websiteB, does jsonp
actually mean websiteA passes the callback to websiteB, and websiteB actually invokes the callback (meaning essentially, websiteB is calling websiteA)?
Question 2 (the main question) : How do I post information from websiteA to websiteB using javascript/ajax/jquery. Or will I have to enable CORS on websiteB server and then use a ajax's post and json?