Click here to Skip to main content
14,920,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to HTML5 web development and I am trying to access a simple asmx web-service using jQuery. The asmx web service method has 2 input parameters (Activity_Type, Activity_Details) and it is used to insert data into database. The web service works fine. I have written the below 2 jQuery functions that are used to call the web service method. I am calling the logToDBJquery function in button click event of an HTML5 page. The web service and the html5 page are using different port numbers 5331 and 6960.

JavaScript
function CallService() {
    $.ajax({
        type: varType,
        url: varUrl,
        data: varData,
        contentType: varContentType,
        dataType: varDataType,
        processData: varProcessData,
        success: function () {
            alert("success");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}


JavaScript
function logToDBJquery(activity_Type, activity_Details) {
    varType = "POST";
    varUrl = "http://localhost:5331/Service1.asmx?op=insertActivityLog";
    varData = '{"Activity_Type":"' + activity_Type + '","Activity_Details":"' + activity_Details + '"}';
    varContentType = "application/json; charset=utf-8";
    varDataType = "json";
    varProcessData = "true";
    CallService();
}


I am constantly getting the below errors. The alert shows 404 and debugger shows below.

1. OPTIONS http://localhost:5331/Service1.asmx?op=insertActivityLog 405 (Method Not Allowed) jquery-2.0.3.js:7845
1. sendjquery-2.0.3.js:7845
2. jQuery.extend.ajaxjquery-2.0.3.js:7301
3. CallServicestickyNote.js:162
4. logToDBJquerystickyNote.js:186
5. createStickyNotestickyNote.js:27
XMLHttpRequest cannot load http://localhost:5331/Service1.asmx?op=insertActivityLog. Origin http://localhost is not allowed by Access-Control-Allow-Origin.


Things I have tried :-

1) Enabled CORS at IIS7 by adding a web.config file and enabling CORS in it.
2) In the varUrl I have tried different options :- "http://localhost:5331/Service1.asmx?wsdl , "http://localhost:5331/Service1.asmx?insertActivityLog
3) Enabled [System.Web.Script.Services.ScriptService] attribute in web service method.
4) Tried variations in VarData parameter.

is there any problem in VarData parameter? I expect a problem in the url but I am not sure what it is.
What am I doing wrong?Please advise.
Thanks in advance.

Regards
Jashobanta
Posted

1 solution

Instead of the above two function optimize the code as the following function.I think it helps you

JavaScript
function InsertActivityLog(activity_Type, activity_Details) {
    $.ajax({
        type: "POST",
        url: "http://localhost:5331/Service1.asmx?op=insertActivityLog",
        data: ({Activity_Type:activity_Type, 
		 Activity_Details:activity_Details}),
        contentType "json",
        processData: "true",
        success: function () {
            alert("success");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}



Thanks
Ipsita
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900