0

I get this error when I click the button in HTML to use my JavaScript.

myapp.azurewebsites.net/:1 Uncaught SyntaxError: Unexpected end of JSON input
 at JSON.parse (<anonymous>)

JS:

function ajaxFunction() {
 console.log("ajaxFunction()");
 var ajaxRequest;
 try {
 ajaxRequest = new XMLHttpRequest();
 } catch(e) {
 try {
 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
 } catch(e) {
 try {
 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
 } catch(e) {
 alert("Broken");
 return false;
 }
 }
 }
 ajaxRequest.onreadystatechange = function () {
 if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
 var form = document.getElementById("myform");
 var titleParagraph = document.getElementById("home_title");
 titleParagraph.innerHTML = ajaxRequest.responseText;
 form.style.display = "none";
 }
 } 
 var value1 = document.getElementById("field1").value;
 var value2 = document.getElementById("field2").value;
 var url = "https://webapp.azurewebsites.net/users/" + value1 + "/" + value2;
 console.log(url);
 // Call sign in web service
 ajaxRequest.open("GET", url, true);
 ajaxRequest.send(null);
 var jsonResponse = JSON.parse(ajaxRequest.responseText);
 console.log("RESPONSE:");
 console.log(jsonResponse);
}

This JSON is what I get when I go to https://webapp.azurewebsites.net/users/value1/value2

[{"ID":1,"UserName":"username","FirstName":"first","LastName":"last","email":"[email protected]"}]
asked Jul 6, 2017 at 19:32
3
  • What is the actual value of ajaxRequest.responseText? Commented Jul 6, 2017 at 19:35
  • @JordanRunning see my update. Commented Jul 6, 2017 at 19:38
  • try checking for ajaxRequest.responseText in the readystatechange handler you set up (inside readyState == 4 && status == 200 block) Commented Jul 6, 2017 at 19:48

1 Answer 1

1
ajaxRequest.open("GET", url, false);

Third parameter says if the request is asynchronous or not. So by looking at your code it should be synchronous.

answered Jul 6, 2017 at 19:41

2 Comments

When I change that, I get No 'Access-Control-Allow-Origin' header is present on the requested resource
Use setRequestHeader() to add that into it

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.