Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
JavaScript
<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.
Posted
Updated 9-Jan-14 3:34am
v2

1 solution

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:
JavaScript
data: JSON.stringify({routeID: routeById, mA: mA, mB: mB}),
 
Share this answer
 
Comments
[no name] 9-Jan-14 9:44am    
Pete: Thanks for your advice. But I received an error: Microsoft JScript runtime error: 'JSON' is undefined. How to handle it?
Pete O'Hanlon 9-Jan-14 9:47am    
You can reference the JSON script, which you can download from http://www.json.org/js.html.
[no name] 9-Jan-14 9:57am    
Per your advice, I got JSON.js from https://github.com/douglascrockford/JSON-js/blob/master/json.js. In my .aspx, I added it
<script src="Scripts/JSON.js" type="text/javascript"></script>
But when I run debugging, got Microsoft JScript runtime error: 'undefined' is null or not an object. Any new advice? Thanks.
Pete O'Hanlon 9-Jan-14 10:01am    
Well, one that that stands out is that you should be using POST rather than GET and remove the brackets from the method name in your URL.
[no name] 9-Jan-14 10:06am    
Changed like that
$.ajax( {
type:'POST',
url: 'SOEDefault.aspx/GetData(routeById, mA, mB)',
data: JSON.stringify(routeByID:routeById, mA:mA, mB:mB),
...
But still get Microsoft JScript runtime error: 'undefined' is null or not an object

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