1

is it possible to post data from an html page using Jquery to another asp.net website ?

say www.site1.com/mail.html - > www.site2.com/mailServ.aspx

using jquery ajax i could use this code to post to code behind [webmethod]

so instead of code within same website(site2) it will be sent from site1

this is the code i am using to post the form data to a web method within same website application

function jQuerySendMailCsCodeBehind(resluts) {

    var SentClientinfo = []
    SentClientinfo.push({ key: "SentClientinfo", value: resluts });
    var CurrpageURL = "default.aspx/"; <---
    var WebmethodName = "StartTest";
    var StageIdentifyer = "stage1";
    var Post_TargetUrl = CurrpageURL + WebmethodName;

    jQueryAajaxNoPostBack(Post_TargetUrl, SentClientinfo, StageIdentifyer);
}

i tried to post from outside of application

so i just used

    var CurrpageURL = "http://www.site2.com/default.aspx/"; 

from the other site (site1) html website non asp.net but the idea did not work in reality (:

so is there an option that a webForms application/ asp.net website will accept requests from another website code of ajax/jquery ?

2 Answers2

1

You cannot make cross-website requests with javascript. You need to use jsonp

jQuery supports jsonp, see the following example

$.ajax({
    type: 'GET',
    url: 'http://githubbadge.appspot.com/badge/torvalds',
    dataType: 'jsonp',
    success: function(json) {
        var result = '<h3>' + json.user.login + '</h3>' +
                     '<p>Languages: ' + json.languages + '</p>' +
                     '<p>Followers: ' + json.user.followers + '</p>';
        $('#badge').append(result);
    }
});
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • thanks . i was visiting both links, could you post a code implementing what is explained there(i have trubles understanding whats writen, i suppose this should be simple example code) ? like site1 jasonp vs accepting site2 jsonp say: **site1** is posting its `('#TBX_UserName').val()` to **site2** – Regina Jerusalemsky Jun 23 '13 at 20:42
  • thanks for the information you have been very helpfull though this would be a partial solution becuse i will need to listen with site2 to site1 at all times to check if data is being posted, does `CORS` gives us a better option? i need to learn some more to check what exactly its implementations and limitations – Regina Jerusalemsky Jun 23 '13 at 23:37
  • @ReginaJerusalemsky you can get more info here http://stackoverflow.com/a/6879276/340760 There is an example using a Twitter jsonp url... The server should return `method( normal_json_response )`. Then you just inject a script element with the remote url, when it loads it will execute a method passing the json parameter – BrunoLM Jun 24 '13 at 02:52
1

By default, JavaScript is not allowed to access other domains than the one it originated from for security reasons. You don't want the JavaScripts on my site to access your bank's web site, with your bank login cookie if you happen to be looking at my site while being logged in to the bank.

One way to work around it is JsonP, but as far as I've understood it it's mostly for retrieving data.

What you're probably looking for is Cross Origin Resource Sharing or short CORS. To implement that, your site2 would need to set a Access-Control-Allow-Origin header and the users would be required to use a browser that supports CORS (not all do, see the wikipedia page for info).

Anders Abel
  • 67,989
  • 17
  • 150
  • 217