JQuery - CORS sample using "POST" with C# Web Service

by Luis Valle

HTML

<div>
    <input id="txtInNumber" type="text" value="2"></input>
    <input id="btnSendVal" type="submit" value="Send Value"></input>
</div>

JavaScript

function sendValue() {
    try {
        jQuery.support.cors = true;
        $.ajax({
            crossDomain: true,
            url: "https://www.evicore.net",
            data: JSON.stringify({'webMethod': 'callMyMethod' }),
            type: "POST",
            dataType: "json",  
            contentType: 'application/json; charset=utf-8',   
            success: function(data)
            {
                //alert(JSON.stringify(data));
                data = (data.hasOwnProperty('d') ? $.parseJSON(data.d) : data);
                
                alert(JSON.stringify(data)); //.resulting
            },
            error: function(jqXHR,textStatus,errorThrown)
            {
                alert(JSON.stringify(jqXHR) + errorThrown);
            }
        });
    } catch (e) {
        alert(e);
    }
    
}

$(document).ready(function()
{
    $('#btnSendVal').on('click', function () {
        //sendValue();
        
        var sparams = {
            UserName: 'egfgdfd',
            Password: 'dhgshfsh'
        };
        
        AJAXcall('authenticateLogin', sparams)
        .then(function (response) {
            if (response.error == '1') {
                alert(response.errorCode);
            } else {
                alert(response.error);
            }
            
            return response;
        });
    });
});

function AJAXcall(spParams) {
    jQuery.support.cors = true;
    var d = $.Deferred();
    
    $.ajax({
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        url: "https://www.evicore.net:8080",
        data: JSON.stringify(spParams),
        type: "POST",
        dataType: "json"
    })
    .done(function(response){
        d.resolve((response.hasOwnProperty('d') ? $.parseJSON(response.d) : response));
    })
    .fail(function (jqXHR, textStatus, errorThrown) {
        //d.reject;
        //alert('Ajax communication failed!\n\n(Error Thrown: "' + errorThrown + '")');
        errorThrown = 'Ajax...