1
Good morning, I am trying to send a POST request to a URL but am having the following error:
Syntaxerror: Unexpected end of JSON input JSON is correct in formatting because I have validated it. The code is like this:
function salvaDados(){
var myData = JSON.stringify('{ "name":"John", "age":30, "city":"New York"}')
$.ajax({
url: "http://localhost:81/adm/dados",
type: "POST",
dataType: 'json',
contentType: 'application/json',
data: myData,
success: function(data){
alert('ok')
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown)
}
})
}
the REST server is normal because I can save this same JSON via Postman.
Face the method JSON.stringify() is used to turn a valid Json into a
string
, note that what you are inserting into the method is already a string!– LeAndrade
Thanks for the answer, when I do JSON.parse, the same error happens.
– user33105
What @Leandrade said is that you should remove
'
in the example you gave, because you are trying to convert a string and not an object... testvar myData = JSON.stringify({ "name":"John", "age":30, "city":"New York"});
– Sergio
@Sergio, I did as you said and the error changed, now it is giving (Syntaxerror: Unexpected token < in JSON at position 0)
– user33105