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":"Birlasoft Ltd.","Address":"LosAngeles California","Phone":"1204675","Country":"US"},{"Name":"Ajay Singh","Company":"Birlasoft Ltd.","Address":"D-195 Sector Noida","Phone":"1204675","Country":"India"}]</string>

Then I run it I get:
Error: [object].[object]

Any suggestions?
doctor069Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
First, your data needs to be wrapper in {}. And secondly, error event handler should be changed to something like error: function(xhr, textStatus, errorThrown) {...}.

Try this code, and let's us know the exact error you get if it still doesn't work:
                $.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 (xhr, textStatus, errorThrown) {
                         alert('Message: ' + textStatus + '; Error: ' + errorThrown)
                     }
                });

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
doctor069Author Commented:
Hi -

This is the data. Do I need to remove the []

[{"Name":"Ajay Singh","Company":"Birlasoft Ltd.","Address":"LosAngeles California","Phone":"1204675","Country":"US"},{"Name":"Ajay Singh","Company":"Birlasoft Ltd.","Address":"D-195 Sector Noida","Phone":"1204675","Country":"India"}]
Duy PhamFreelance IT ConsultantCommented:
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/.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

doctor069Author Commented:
Hi - Duy

I tried your code and get the error "Message: error; Error"
Duy PhamFreelance IT ConsultantCommented:
Try to use Developer Tools (F12) in Chrome or Internet Explorer to see what is the error. Something like in below screenshot from Chrome (same in Internet Explorer Developer Tools - Console tab):
Sample error
doctor069Author Commented:
I am getting Cross-Origin Request Blocked
Duy PhamFreelance IT ConsultantCommented:
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:
        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();
            }
        }

Open in new window


More about enabling CORS can be found here: http://enable-cors.org/.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.