ikeyser
asked on
jQuery Ajax Post Json
Hi Experts. My function below is suppose to return json data that I am using to populate a div. When I look at Fiddler. My webservice call is passing the json data back correctly. What am I missing?
function OnLookup() {
$.ajax({
type: "POST",
url: "http://<%=HttpContext.Current.Re quest.Url. Host%>/ser vices/Loca tionServic e.asmx/Cha ngeLocatio n",
data: "{zipcode : '" +$('#edtzipcode').val()+"' }",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data){
alert(data);
$('#curlocation').innerHTM L(data[1]. City + "," + data[1].State);
$('#edtzipcode').val("");
},
});
}
function OnLookup() {
$.ajax({
type: "POST",
url: "http://<%=HttpContext.Current.Re
data: "{zipcode : '" +$('#edtzipcode').val()+"'
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data){
alert(data);
$('#curlocation').innerHTM
$('#edtzipcode').val("");
},
});
}
ASKER
The JSON data returns...
{"d":{"DataObject":{"Locat ionId":8," EntityId": 9,"EntityT ypeId":2," LocationNa me":"loc1" ,"Address1 ":"1495 S. Wolf Rd.","Address2":null,"City ":"Prospec t Heights","ZipCode":"60070" ,"Modified By":2,"Mod ifiedDateT ime":"\/Da te(1279386 329023)\/" ,"CreatedB y":2,"Crea tedDateTim e":"\/Date (127938632 9023)\/"," LocationId entifierId ":null,"St ate":"IL", "EntitySta te":1,"Ent ityKey":{" EntitySetN ame":"Loca tion","Ent ityContain erName":"R esourceDBE ntities"," EntityKeyV alues":[{" Key":"Loca tionId","V alue":8}], "IsTempora ry":false} },"Address 1":"1495 S. Wolf Rd.","Address2":null,"City ":"Prospec t Heights","LocationName":"l oc1","Stat e":"IL","Z ipCode":"6 0070","Ent ityId":9," EntityType Id":2,"Has Changed":f alse,"IsVa lid":true, "Validatio nMessages" :[]}}
{"d":{"DataObject":{"Locat
What about : data: { zipcode : $('#edtzipcode').val() },
(you post a json object and not a string)
(you post a json object and not a string)
function OnLookup() {
$.ajax({
type: "POST",
url: "http://<%=HttpContext.Current.Request.Url.Host%>/services/LocationService.asmx/ChangeLocation",
data: { zipcode : $('#edtzipcode').val() },
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data){
alert(data);
$('#curlocation').innerHTML(data[1].City + "," + data[1].State);
$('#edtzipcode').val("");
},
});
}
ASKER
Sorry new to this...my web service is expecting a string value which is what I am passing.
ASKER
Leakim971 - When I changed to data : { zipcode : $('#edtzipcode').val() },
I get an error when I make my ajax call...
{"Message":"Invalid JSON primitive: zipcode.","StackTrace":" at System.Web.Script.Serializ ation.Java ScriptObje ctDeserial izer.Deser ializePrim itiveObjec t()\r\n at System.Web.Script.Serializ ation.Java ScriptObje ctDeserial izer.Deser ializeInte rnal(Int32 depth)\r\n at System.Web.Script.Serializ ation.Java ScriptObje ctDeserial izer.Basic Deserializ e(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serializ ation.Java ScriptSeri alizer.Des erialize(J avaScriptS erializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serializ ation.Java ScriptSeri alizer.Des erialize[T ](String input)\r\n at System.Web.Script.Services .RestHandl er.GetRawP aramsFromP ostRequest (HttpConte xt context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services .RestHandl er.GetRawP arams(WebS erviceMeth odData methodData, HttpContext context)\r\n at System.Web.Script.Services .RestHandl er.Execute WebService Call(HttpC ontext context, WebServiceMethodData methodData)","ExceptionTyp e":"System .ArgumentE xception"}
I get an error when I make my ajax call...
{"Message":"Invalid JSON primitive: zipcode.","StackTrace":" at System.Web.Script.Serializ
success: function(data){
data = $.parseJSON(string);
$('#curlocation').innerHTM
$('#edtzipcode').val("");
},
yopu need to parse the JSON to actually use the JSON data.
hoep this helps
>I get an error when I make my ajax call...
Are using DataContract & Co ? What is the language used for the webservice ? VB.Net or C#.Net
Are using DataContract & Co ? What is the language used for the webservice ? VB.Net or C#.Net
ASKER
shinug - the .ajax call automatically parses the json data. After debugging, I found the solution.
data[1].City does not exist. What i ended up doing is
success: function(data){
result = data.d;
$('#curlocation').innerHTM L(result.C ity + "," + result.State);
$('#edtzipcode').val("");
},
data[1].City does not exist. What i ended up doing is
success: function(data){
result = data.d;
$('#curlocation').innerHTM
$('#edtzipcode').val("");
},
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There is no return value if the zipcode is valid.
Hope this helps
Hope this helps
function OnLookup() {
$.ajax({
type: "POST",
url: "http://<%=HttpContext.Current.Request.Url.Host%>/services/LocationService.asmx/ChangeLocation",
data: "{zipcode : '" +$('#edtzipcode').val()+"'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function OnLookupComplete(result) {
data = result.d;
$("#curlocation").text(data.City + "," + data.State);
$("#edtzipcode").val("");
},
});
return true;
}
ASKER
Shinug - #curlocation is the field I am populating, not the zipcode field.
http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?
Note the ({ in the beginning and }) in the end.