无法使用参数调用WebMthod [英] Could not call a WebMthod with parameters

查看:67
本文介绍了无法使用参数调用WebMthod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主题的开发延续

使用JQuery从WebMethod获取数据 [ ^ ]

在上一个场景中,我创建了一个WebMethod GetData()。下面的代码工作正常

< button id =   submitButton data-dojo-type =   dijit.form.Button数据 - dojo-props =   onClick:function(){
var e = document.getElementById('routeID') ;
var routeById = e.options [e.selectedIndex] .text;
var mA = document.getElementById('MeasureA')。value;
var mB = document.getElementById('MeasureB ')。value;
调试器;
$ .support.cors = true;
var ajaxData;
$ .ajax({
类型:'获取',
url:'SOEDefault.aspx / GetData()',
dataType:'text',
data:'{}',
contentType:'application / json; charset = utf -8',
crossDomain:true,
success:function(results){
console.log('Success!');
调试器;
ajaxData = results;
},
错误:函数(xhr,状态,错误){
debugger;
console.log(错误);
}
});

...

现在我为GetData添加了三个参数(routeID,mA,mB)' 。 3个参数是文本框或下拉列表中的值。我还在.aspx.cs中修改了带有3个参数的GetData方法。但是,当我单击按钮时,我收到内部服务器错误。请告诉我我的JavaScript代码有什么问题。谢谢。

解决方案

.support.cors = true;
var ajaxData;


.ajax({
type:'Get',
url:'SOEDefault.aspx / GetData()',
dataType:'text',
data:'{}',
contentType:'application / json; charset = utf-8',
crossDomain:true,
success :function(results){
console.log('Success!');
debugger;
ajaxData = results;
},
error:function(xhr,状态,错误){
调试器;
console.log(错误);
}
});

...

现在我为GetData(routeID,mA,mB)添加了三个参数。 3个参数是文本框或下拉列表中的值。我还在.aspx.cs中修改了带有3个参数的GetData方法。但是,当我单击按钮时,我收到内部服务器错误。请告诉我我的JavaScript代码有什么问题。谢谢。


您实际上并没有向ajax调用提供任何数据。您需要将 dataType 设置为 json 而不是 text ,并将数据设置为:

 data: JSON  .stringify({routeID:routeById,mA:mA,mB:mB}),


This is the development continuation of my thread
Get Data from WebMethod Using JQuery[^]
In previous scenario, I created a WebMethod GetData(). The code below works fine

<button id="submitButton" data-dojo-type="dijit.form.Button" data-dojo-props="onClick:function(){
            var e = document.getElementById('routeID');
            var routeById = e.options[e.selectedIndex].text;
            var mA = document.getElementById('MeasureA').value;
            var mB = document.getElementById('MeasureB').value; 
            debugger; 
            $.support.cors = true;
            var ajaxData;
            $.ajax( {
                  type:'Get',
                  url: 'SOEDefault.aspx/GetData()',
                  dataType: 'text',
                  data:  '{}',
                  contentType: 'application/json; charset=utf-8',
                  crossDomain : true,
                  success:function(results) {
                    console.log('Success!');
                    debugger; 
                    ajaxData = results;
                  }, 
                  error: function(xhr, status, error) { 
                    debugger;
                    console.log(error);
                  }
                });

...
Now I added three parameters for the GetData(routeID, mA, mB)'. The 3 parameters are the values from textboxes or dropdownlist. I also revised the GetData methods with 3 parameters in .aspx.cs. However, when I click the button, I got "Internal Server Error". Please advise me what's wrong in my JavaScript code. Thanks.

解决方案

.support.cors = true; var ajaxData;


.ajax( { type:'Get', url: 'SOEDefault.aspx/GetData()', dataType: 'text', data: '{}', contentType: 'application/json; charset=utf-8', crossDomain : true, success:function(results) { console.log('Success!'); debugger; ajaxData = results; }, error: function(xhr, status, error) { debugger; console.log(error); } });

...
Now I added three parameters for the GetData(routeID, mA, mB)'. The 3 parameters are the values from textboxes or dropdownlist. I also revised the GetData methods with 3 parameters in .aspx.cs. However, when I click the button, I got "Internal Server Error". Please advise me what's wrong in my JavaScript code. Thanks.


You aren't actually supplying any data to the ajax call. You need to set your dataType to json rather than text, and set data as:

data: JSON.stringify({routeID: routeById, mA: mA, mB: mB}),


这篇关于无法使用参数调用WebMthod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆