doctor069
asked on
Jquery Json and Ajax
Hi - I am trying to get a singe value from a web service in a json format. My code looks like this
function accountLookup() {
$.ajax({
type: "GET",
url: "http://testserver/customerinformation.asmx/TestJSON",
data: " get_param: 'Name' ",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('Good:' + data)
},
error: function (msg) {
alert('Error:' + msg)
}
});
}
And the web service output looks like this (.net)...
-<string>[{"Name":"Ajay Singh","Company":"Birlasof t Ltd.","Address":"LosAngele s California","Phone":"12046 75","Count ry":"US"}, {"Name":"A jay Singh","Company":"Birlasof t Ltd.","Address":"D-195 Sector Noida","Phone":"1204675"," Country":" India"}]</ string>
Then I run it I get:
Error: [object].[object]
Any suggestions?
function accountLookup() {
$.ajax({
type: "GET",
url: "http://testserver/customerinformation.asmx/TestJSON",
data: " get_param: 'Name' ",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('Good:' + data)
},
error: function (msg) {
alert('Error:' + msg)
}
});
}
And the web service output looks like this (.net)...
-<string>[{"Name":"Ajay Singh","Company":"Birlasof
Then I run it I get:
Error: [object].[object]
Any suggestions?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No, you shouldn't remove the [], it's indicating a json array. Just parse that data into an array of json objects and work around with them as you want.
See how you parse that data into JSON and work it them here: http://jsfiddle.net/hx8gbfzx/.
See how you parse that data into JSON and work it them here: http://jsfiddle.net/hx8gbfzx/.
ASKER
Hi - Duy
I tried your code and get the error "Message: error; Error"
I tried your code and get the error "Message: error; Error"
ASKER
I am getting Cross-Origin Request Blocked
That issue is because you are trying to call web service hosted in different domain than your current domain hosting your web application. See description about CORS here: http://www.w3.org/TR/cors/.
Try to change dataType: "json" to dataType: "jsonp" to see if it could help. If it doesn't you might need to enable CORS on your Web Service.
To enable CORS in your ASMX Web Service, modify your Global.asax.cs as below:
More about enabling CORS can be found here: http://enable-cors.org/.
Try to change dataType: "json" to dataType: "jsonp" to see if it could help. If it doesn't you might need to enable CORS on your Web Service.
To enable CORS in your ASMX Web Service, modify your Global.asax.cs as below:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Origin, Content-Type, Accept");
HttpContext.Current.Response.End();
}
}
More about enabling CORS can be found here: http://enable-cors.org/.
ASKER
This is the data. Do I need to remove the []
[{"Name":"Ajay Singh","Company":"Birlasof