Updated: jQuery to C# – AJAX (with data)

[WebMethod]
public static string testingWebMethod(string name) {

  //Do something with name? 

  string someString = "Woah";

  return someString;
}
var dataToSend = JSON.stringify({
  "name": "foo"
});

$.ajax({
  type: "POST",
  url: "page.aspx/testingWebMethod",
  dataType: "json",
  data: dataToSend,
  contentType: "application/json",
  success: function(response) {
    alert(response.d);
  },
  error: function(err) {
    alert(err.d);
  }
});
Advertisements

Testing Javascript and jQuery array loops

(by axds)

https://jsfiddle.net/s7bn7cfr/

The simple for loop seems to win the speed tests:

let arrLen = test.length; for( let i = 0; i < arrLen; i++ ){  }

Although this method is less DRY as the length of the the array has to be known. JS’s forEach() or jQuery’s $.each() are good alternatives that are dynamic.

The clear worst is the ‘for x in y’ loop. Which has bulky syntax and is slow.

 

 

 

ES2015 (ES6) Template Strings

Example: https://jsfiddle.net/ehdgpvyb/

ES2015 provides a better way to use variables inside strings.


function exampleJS5( name , age ) {
console.log("Hello " + name + ", you are " + age + "!");
}

function exampleJS6( name , age ) {
console.log(`Hello ${name}, you are ${age}!`);
}

exampleJS5('Bob', 20);
//Hello Bob, you are 20!
exampleJS6('Bob', 20);
//Hello Bob, you are 20!

Review: Ajax POST to C# WebMethod

[WebMethod]
public static void testingWebMethod(Object dataTest)
{
Debug.WriteLine(dataTest);

Debug.WriteLine(dataTest);
}

 

var sendThisData = JSON.stringify({
dataTest: [{ "name": "foo" }]
});
$.ajax({
type: "POST",
url: "page.aspx/testingWebMethod",
data: sendThisData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
},
error: function () {
alert("Something went wrong.");
}
});