1

I am kind of new to JavaScript library's. I wanted to replace my current code with a JS lib jQuery. My current code looks like this.

function myajax() 
		{
		var xmlhttp = new XMLHttpRequest();
		var url = "http://localhost:8080/Marker1/webresources/org.sample.marker/markerlist";
		xmlhttp.onreadystatechange = function () {
		    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
			{
			myFunction(xmlhttp.responseText);
		    }
		};
		xmlhttp.open("GET", url, true);
		xmlhttp.send();
	    }
    function myFunction(data) {
            myArr = JSON.parse(data);   // parsing JSON
	updateMap(data);   
    }

1
  • i am using GET method only Commented Sep 2, 2015 at 6:41

4 Answers 4

1

You can use jQuery.get():

var url = "http://localhost:8080/Marker1/webresources/org.sample.marker/markerlist";
$.get(url, function(data) {
    myArr = JSON.parse(data);   // parsing JSON
    updateMap(data);
});

The anonymous function will be the callback. Of course, you can keep it in a separate declaration, as you had it:

function myFunction(data) {
    myArr = JSON.parse(data);   // parsing JSON
    updateMap(data);   
}

and just use the pointer to it at the $.get() call:

$.get(url, myFunction);
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like this:

$.ajax({
  url: 'http://localhost:8080/Marker1/webresources/org.sample.marker/markerlist',
  type: 'GET',
  success: function(responseText){
   var jsonData = JSON.stringify(responseText); 
    myArr = JSON.parse(jsonData );   // parsing JSON
    updateMap(myArr); 
  }
});

More information about the ajax function in the jquery-API

Comments

0

Jquery doco

$.get( "http://localhost:8080/Marker1/webresources/org.sample.marker/markerlist", function( data ) {
   myFunction(data);
});

Comments

0

Is this what you are looking for?

Post:

$.post(url, jsonData, function(res) { 
   //res what is returned from the server 
});

Get:

$.get(url, jsonData, function(res) { 
   //res what is returned from the server 
});

Ajax:

$.ajax({
  type: "POST",
  url: url,
  data: jsonData,
  success: successMethod,
  dataType: dataType
});

Links:

jQuery post
jQuery get
jQuery ajax

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.