Hi everyone i am working on phone gap project and using the ajax web services. I am sending request using the following code:
var credentials = {
"name": "nouman",
"email": "noumandilawar#gmail.com",
"mobile_number": 03324412764,
"employee_number": 4556,
"gender": 1,
"password": "1234567891234567",
"language": 1
};
function GetMember() {
$.ajax({
type: 'POST',
url: 'http://192.168.1.103:8080/mazaya_cms/signup.htm',
//data: "{'name': 'nouman','email': 'noumandilawar#gmail.com','mobile_number': 03324412764,'employee_number': 4556, 'gender': 1,'password': '1234567891234567','language': 1}",
data: JSON.stringify(credentials),
//data: '{"id": "nouman","name":"nouman"}',
contentType: "application/json; charset=utf-8",
dataType: 'json',
crossDomain: true,
success: OnGetMemberSuccess,
error: OnGetMemberError
});
}
GetMember();
function OnGetMemberSuccess(data, status) {
alert(data+ " "+status);
}
function OnGetMemberError(request, status, error) {
alert(request+" "+error+ " "+status);
}
I am getting error code:415 that is unsupported media type Please help me!
I have successfully solved this issue
var credentials = {
"name": "nn",
"email": "nn%40gmail.com",
"mobile_number": 03324412764,
"employee_number": 4556,
"gender": 1,
"password": "1234567891234567",
"language": 1
};
$.ajax({
url: "http://192.168.1.103:8080/mazaya_cms/signup.htm",
type: "post",
data: credentials,
dataType : "json",
success: function(request, status, error){
alert("success "+request.status);
},
error:function(jqXHR, textStatus, errorThrown) {
alert("failure"+errorThrown);
}
});
Related
I'm using ajax and javascript for a game and I created a server using json-server where I keep a db.json file with words that the user can input and become available in the game.
db.json
This is what the json file looks like
{
"words": [
{
"cuvant": "cuvant",
"id": 1
},
{
"cuvant": "masina",
"id": 2
},
{
"cuvant": "oaie",
"id": 3
},
{
"cuvant": "carte",
"id": 4
},
{
"cuvant": "fmi",
"id": 5
},
{
"cuvant": "birou",
"id": 6
},
{
"cuvant": "birou",
"id": 7
},
{
"cuvant": "canapea",
"id": 8
},
{
"cuvant": "pasare",
"id": 9
I managed to get the POST request (adding the words to the db.json) using this:
function addWord() {
var word = document.getElementById("input-name").value;
var info = {
cuvant: word
}
$.ajax({
url:'http://localhost:3000/words',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(info),
succes: function(info) {
console.log(info)
},
error: function(error) {
console.log(error);
}
})
}
This is what I tried for the GET request
But I'm not sure if it's correct.
And I also need a way to get only the value hold by the cuvant key and add it into an array.
window.onload = function() {
function gett() {
$.ajax({
url: 'http://localhost:3000/words',
type: 'GET',
dataType: 'json',
contentType: "application/json",
succes: function(data) {
console.log(data);
},
error: function(data) {
console.log(data)
}
})
.done(function(){
console.log("Over")
})
}
}
I think your code is correct. It was just missing the "s" on "success":
window.onload = function() {
function gett() {
$.ajax({
url: 'http://localhost:3000/words',
type: 'GET',
dataType: 'json',
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data)
}
})
.done(function(){
console.log("Over")
})
}
}
When I try to pass my json object for creating a task item and pass the content-type application/json-patch+json as well and the type PATCH i'm getting an erro: The request indicated a "Content-Type of \"\" for method type \"PATCH\" which is not supported. Valid content types for this method are application/json=patch+json. ".
It works fine when I call it in Postman with the same application type and type Patch. I'm trying to create a webpage and get this to call the service when I click a button on the form.
var newJson = '[{"op":"add","path":"/fields/System.Title","value":"JavaScript implementation for Microsoft Account"}]';
var oJson = JSON.stringify(newJson);
//AZURE URL
var url = "https://dev.azure.com/AIZ-GL-dryrun/375977db-f390-4aac-bc4d-808f51360f9a//_apis/wit/workitems/$task?api-version=5.1";
jQuery.support.cors = true;
$.ajax({
url: url,
async: true,
data: oJson,
type:'PATCH',
contentType: "application/json-patch+json;",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic "+btoa(username+':'+password));
},
success: function(data){
var myObject = JSON.stringify(data);
alert("RESULT: "+myObject);
},
error: function(err) {
alert(JSON.stringify(err))}
});
As Daniel pointed out, var url = "https://dev.azure.com/AIZ-GL-dryrun/375977db-f390-4aac-bc4d-808f51360f9a//_apis/wit/workitems/$task?api-version=5.1"
You were trying to use API with version 5.1. As you can see, this version is not supported with Team Foundation Server 2015.
Need to use version 2.x on TFS 2015 and try again. More detail about api version, kindly take a look at this official link-- REST API Versioning
Update, use post instead of patch and try again.
return $.ajax({
url: "https://{account}.visualstudio.com/DefaultCollection/{project}/_apis/wit/workitems/$Bug?api-version=1.0",
type: "POST",
headers: {
"Authorization": "Basic " + btoa("{user}:{password}"),
"X-HTTP-Method-Override": "PATCH",
"Content-Type": "application/json-patch+json; charset=utf-8"
},
dataType: 'json',
cache: false,
data: JSON.stringify(myData),
async: false,
error: function (jqXHR, textStatus, errorThrown) {
//alert(jqXHR.status + ': ' + errorThrown);
},
success: function (data) {
// alert(JSON.stringify(data));
}
});
Another way, you could also use Work item batch api instead:
For example:
http://[collection url]/_apis/wit/$batch?api-version=1.0
Body:
[
{
"method": "PATCH",
"uri": "/ScrumStarain/_apis/wit/workItems/$Product Backlog Item?api-version=1.0",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": "add",
"path": "/fields/System.Title",
"value": "apip1"
},
{
"op": "add",
"path": "/id",
"value": "-1"
}
]
},
{
"method": "PATCH",
"uri": "/ScrumStarain/_apis/wit/workItems/$Task?api-version=1.0",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": "add",
"path": "/fields/System.Title",
"value": "apip2"
},
{
"op": "add",
"path": "/id",
"value": "-2"
}
]
}
]
More information, you can refer to: Work item batch operations
Im trying to access a API and heres the tree i want to access:
{"19777621": [{
"queue": "RANKED_SOLO_5x5",
"name": "Vladimir's Maulers",
"entries": [{
"leaguePoints": 0,
"isFreshBlood": false,
"isHotStreak": true,
"division": "I",
"isInactive": false,
"isVeteran": false,
"losses": 34,
"playerOrTeamName": "Razdiel",
"playerOrTeamId": "19777621",
"wins": 36
}],
"tier": "PLATINUM"
}]}
I managed to do a lot of examples but this is the one i really cant figure out how it works, im sure i can the response body but if i try to do something it comes as undefined blank or Object.
<head>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="/js/json2.js"></script>
<script src="/js/json_parse.js"></script>
</head>
<body>
<script>
$.ajax({
url: 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/19777621/entry?api_key=b05c2777-462b-4bcc-ac2a-a3223bb74876',
type: 'GET',
dataType: 'json',
data: {
},
success: function (json) {
document.write("The Result Is:")
JSON_Encoded = json;
JSON_Decoded = JSON.stringify(json);
document.write(JSON_Decoded[19777621].name[0])
document.write(JSON_Decoded[19777621].entries.losses[0])
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner data!");
}
});
</script>
I know im doing something wrong i just wanted to know what
You could write it in this way ...
$.ajax({
url: 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/19777621/entry?api_key=b05c2777-462b-4bcc-ac2a-a3223bb74876',
type: 'GET',
dataType: 'json',
data: {
},
success: function (json){
document.write("The Result Is:")
//JSON_Encoded = json;
//JSON_Decoded = JSON.stringify(json);
document.write(json['19777621'][0].name)
document.write(json['19777621'][0].entries[0].losses)
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner data!");
}
});
I'm making an API call with this code:
$.ajax({
type: "GET",
url: "https://example/example.json",
beforeSend: function(xhr) {
xhr.setRequestHeader("apikey", "user")
},
success: function(data){
alert(data.folder.item);
}
})
That returns nothing, except for an error in the console saying:
"Uncaught TypeError: Cannot read property 'item' of undefined"
The JSON data looks like this when I use the url in the browser:
{
"folder": {
"item": 123123,
"item 2": false,
"item 3": "content",
"item 4": [
{
"subitem": "content"
},
{
"subitem": "content2"
}
]
}
}
I was expecting "123123" in the alertbox, but nope. So what am I doing wrong?
If its a JSON string you are getting it will need to be parsed. Try this:
$.ajax({
type: "GET",
url: "https://example/example.json",
beforeSend: function(xhr) {
xhr.setRequestHeader("apikey", "user")
},
success: function(data){
var parsed = JSON.parse(data);
alert(parsed.folder.item);
}
});
Or to force jquery to parse it for you:
$.ajax({
type: "GET",
url: "https://example/example.json",
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader("apikey", "user")
},
success: function(data){
alert(data.folder.item);
}
});
here is my code
$.ajax({
type: 'GET',
url: "<%=request.getContextPath()%>/manageUsers.do",
cache: false,
data:{ "resultType": "json", "submit": $.i18n.prop('esadmin.manage.users.delete-btn'), "OID": oid },
invokedata: { "OID": oid, "username": username },
contentType: "application/json",
dataType: "text",
success: deleteUserSuccess,
error: deleteUserError
});
Try calling it like this
$.ajax({
type: 'GET',
url: "<%=request.getContextPath()%>/manageUsers.do",
cache: false,
data:{ "resultType": "json", "submit": $.i18n.prop('esadmin.manage.users.delete-btn'), "OID": oid },
invokedata: { "OID": oid, "username": username },
contentType: "application/json",
dataType: "json",
success: deleteUserSuccess,
error: deleteUserError
});