Call cross-domain service from js

Hello All,

Today, i want to share how to call cross-domain service from js.

We may call service from js with ajax call. Following function will return json data from service. Make sure your service also return json data too.

$.getData = function(url, options) {
            $.ajax({
                url: url,
                type: "POST",
                data: JSON.stringify(options),
                dataType: "json",
                async: false,
                success: function(msg) {
                    returnData(options.method, jQuery.parseJSON(msg.result));
                }
            });
        };

Now, to call service write as follow

function getDataFromService() {
        $.getData("service_url/WebServices.ashx", { method: 'method_name', params: { /* if any */ }
        });
    };

To use parameter in your service write as “param_name1″ : param_value, “param_name2″ : param_value…

That’s it. enjoy your service call. Your browser will block cross-domain service so, make sure you will remove that option from browser settings.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>