且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用jquery调用asmx webservice时出错

更新时间:2023-09-13 17:46:40

.ajax({
类型:varType,
url:varUrl,
data:varData,
contentType:varContentType,
dataType:varDataType,
processData:varProcessData,
success : function (){
alert( 成功);
},
错误: function (xhr,ajaxOptions,thrownError){
alert(xhr) .status);
alert(thrownError);
}
});
}
.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); } }); }







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();
}





我经常收到以下错误。警报显示404和调试器显示如下。



1.选项http:// localhost:5331 / Service1.asmx?op = insertActivityLog 405(方法不允许)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无法加载http:// localhost:5331 / Service1.asmx?op = insertActivityLog。 Access-Control-Allow-Origin不允许使用原点http:// localhost。





我尝试过的事情: -



1)通过添加web.config文件并在其中启用CORS在IIS7上启用CORS。

2)在varUrl中,我尝试了不同的选项: - http:// localhost:5331 / Service1.asmx?wsdl,http:// localhost:5331 / Service1.asmx?insertActivityLog

3)启用[System.Web.Script。 Web服务方法中的Services.ScriptService]属性。

4)尝试VarData参数的变化。



VarData参数有什么问题吗?我希望网址有问题,但我不确定它是什么。

我做错了什么?请告知。

提前致谢。



问候

Jashobanta



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


而不是以上两个函数优化代码如下功能我觉得它可以帮到你



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

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
成功: function (){
alert( success);
},
错误: function (xhr,ajaxOptions,thrownError){
alert(xhr.status);
alert(thrownError);
}
});
}
.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); } }); }







谢谢

Ipsita




Thanks
Ipsita