-1
var deneme = document.createElement("SCRIPT");
deneme.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js";
var nodeajax = document.createTextNode("function deneme() {console.log('dawdawdawd');$.ajax({ type: \"POST\", url: \"http://localhost:54371/checkbox.aspx/Check\", data: '{sayfa: 10 }', contentType: \"application/json; charset=utf-8\", dataType: \"json\", success: function (response) { console.log(response.d); alert('sdf'); }, failure: function (response) { console.log(response); } });}"););
deneme.appendChild(nodeajax);
document.body.appendChild(deneme);

How do I define ajax without using script tag in the .js file?

' $ is not defined.' I get an error.

simge sen
  • 41
  • 3

1 Answers1

0

This would be solve your problem

var jqueryScript = document.createElement('script')
var ajaxScript = document.createElement('script')
jqueryScript.src = 'https://code.jquery.com/jquery-3.4.1.min.js'
ajaxScript.innerHTML = `
window.onload = function() {
  $.ajax({
    type: "POST",
    url: "http://localhost:54371/checkbox.aspx/Check",
    data: {sayfa: 10},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
      console.log(response);
      alert('sdf');
    },
    failure: function(response) {
      console.log(response);
    }
  });
}
`
document.body.appendChild(jqueryScript)
document.body.appendChild(ajaxScript)

Here is the codepen link, it is working

Ahmet Zeybek
  • 1,196
  • 1
  • 13
  • 24