0

I have a web service and I'm trying to get data by JSON. if I access within the same server works fine but when I upload it to the hosting it doesn't work. the problem is the "?callback=?" param. with chrome i see the returned json but the code below didn't show it in the input text:

   <html><head> <script src="jquery-1.7.1.min.js"  type="text/javascript" ></script>
    <script>
        $(document).ready(function()
        {   
            $("#cons").click(function(){
                alert("wait");
                var nombre = $("#nombre").attr('value');
                if(nombre!==''){
                    var today = new Date();
                    var mm = today.getMonth()+1; //January is 0!
                    var yyyy = today.getFullYear();

                    $.getJSON("http://grup15.mihost.info/index.php/WS/Api/user/nombre/"+nombre+"/ano/"+ yyyy +"/mes/"+mm+"/format/json?callback=?", function(data) {

                        var htmlResult = "";
                        $.each(data, function(key, val) {
                            htmlResult += val.total;
                        });

                        $('#gasto').attr('value',htmlResult);
                    });
                }else{
                    alert("Nombre Necesario");
                }
            });
        });
    </script>
</head>
<body>
    <table>
        <tr>
            <td>Nombre:</td>
            <td><input type="text" id="nombre" value=""/></td>
        </tr>
        <tr>
            <td>Gasto De este mes:</td>
            <td><input id="gasto" type="text" value=""/></td>
        </tr>
        <tr><td colspan="2"><button id="cons" type="button">Consultar</button></td></tr>
    </table>


</body>

If i delete the "?callback=? chrome shows: "not allowed by Access-Control-Allow-Origin" what i have to do??? thanks for the help!

Roomm
  • 905
  • 11
  • 23
  • 1
    Does your server handle JSONP? The request would need to parse the callback function name and wrap the request in a JavaScript function of that name. – Joe Mar 26 '12 at 20:36

4 Answers4

2

You cannot make AJAX requests cross-domain like this. Use JSONP instead.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
2

?callback=? means JSONP, which means the server should return JSONP, not JSON.

JSONP is actually a script, that looks like so:

func({data: 123, test: 456});

Your data needs to be wrapped in the value of the callback parameter.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • how can i wrap the data in the value of callback parameter?? thanks! – Roomm Mar 26 '12 at 20:42
  • @Roomm: Read value of the `callback` parameter (it's a GET variable), and then print a string like: `func({data:12})`. Here's a PHP example: `echo $_GET['callback'] . '(' . $data . ')';`. – gen_Eric Mar 26 '12 at 20:46
  • 1
    @Roomm: You need to edit the "Web Service" on the __server__ to work with JSONP (in JavaScript on the __client__). – gen_Eric Mar 26 '12 at 20:51
1

Javascript adheres to the "same origin" policy, as described here:

http://en.wikipedia.org/wiki/Same_origin_policy

This is a security measure to prevent cross-site scripting.

You might consider workarounds discussed here: Ways to circumvent the same-origin policy

Community
  • 1
  • 1
Marc
  • 11,403
  • 2
  • 35
  • 45
0

If you can't make a JSONP you could build a wrapper function to fetch your data. Maybe via PHP.

Take a look: craigslist rss feed. My solution is build to work with XML but ist should be easily converted to work with JSON.

Community
  • 1
  • 1
gearsdigital
  • 13,915
  • 6
  • 44
  • 73