0

I have to get a Json object from a web service and I haven´t find any documentation about it. I have to post an sql sentence to get it. Anyone knows if all this is posible?

ok thanks for your answers. At the first the web service has a GetJsonString "metod". Im trying this code:

$(document).ready(function () {
$.ajax({
    type: 'Get',
    url: 'http:********************************.asmx?op=GetJSONString',
    success: function(returnedJson) {
        alert(returnedJson)
    }
    });

});

and I get this error:

XMLHttpRequest cannot load http:/***********asmx?op=GetJSONString. Origin null is not allowed by Access-Control-Allow-Origin.
0

3 Answers 3

1

You can send your SQL as a parameter of the POST request like

$.ajax({
    type: 'POST',
    url: "http://www....",
    method: "serverCallback",
    data: "SELECT ....",
    success: success,
    dataType: dataType
 });

process it with a RPC (remote process call), pack your results in json and the success callback will get it back as a parameter. The downside is it is very dangerous sending raw SQL queries from your web application, it is an easy target for SQL Injection attacks.

So it would be better if you can send a parameter that will define which SQL query will be executed in the database instead of sending the query itself.

Sign up to request clarification or add additional context in comments.

2 Comments

but how can i get de returned data? like this? THanks
For the json struct { name: "john", lastname: "doe" }, you can: function success(data) { console.log( data.name, data.lastname); }
1

Check this: http://www.json.org/js.html

To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.

var myObject = eval('(' + myJSONtext + ')');

the myJSONtext is the return value of your web service

If you are using jQuery, this post is a good answer: Calling a web service in JQuery and assign returned Json to JS variable

3 Comments

but, how can I get that "return value of my web service" ?
the last link gives a method by using jQuery. But as Aaron said, you need to let the web service returns string in a JSON style, like {name:'Jony',titles:[{name:'A'},{name:'B'}]} ...
success: function(data) { //data is the object that youre after, handle it here } the parameter "data" is the "myJSONtext".
1

It is possible but depends on a few factors:

  1. What does the web service return? The standard web service returns XML, not JSON. So you either have to parse the XML directly or convert it to JSON.
  2. Sending SQL to a web service is a huge security risk. Imagine someone sent DELETE FROM important_table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.