使用jquery调用asmx webservice时出错 [英] Error in calling an asmx webservice using jquery
问题描述
我是HTML5网站开发的新手,我正在尝试使用jQuery访问一个简单的asmx web服务。 asmx Web服务方法有2个输入参数(Activity_Type,Activity_Details),用于将数据插入数据库。网络服务工作正常。我编写了以下2个jQuery函数,用于调用Web服务方法。我在HTML5页面的按钮单击事件中调用logToDBJquery函数。 Web服务和html5页面使用不同的端口号5331和6960.
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.
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);
}
});
}
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
推荐答案
.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 < span class =code-string> 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
这篇关于使用jquery调用asmx webservice时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!