tags:

views:

37

answers:

2

Hello,

I like to work with JSON and jquery. When I test the script offline on localhost it works fine, but when I change this json file to a server it doens't work al all

    jQuery(function($) {

    $.ajax
    ({
        type: "GET",
        url: "test.php",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: companySearchSuccess,
        error: onError
    });



function companySearchSuccess(json)
{
  alert("stap1");
var select = $('#city-list');

                $.each(json, function(k, v) {
                        var option = $('<option />');

                        option.attr('value', v)
                              .html(v)
                              .appendTo(select);
                });

}

function onError(m){
alert("Fout is: "+m);
}

});

This works fine,

but this one not:

  jQuery(function($) {

    $.ajax
    ({
        type: "GET",
        url: "http://www.example.com/test.php",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: companySearchSuccess,
        error: onError
    });



function companySearchSuccess(json)
{
  alert("stap1");
var select = $('#city-list');

                $.each(json, function(k, v) {
                        var option = $('<option />');

                        option.attr('value', v)
                              .html(v)
                              .appendTo(select);
                });

}

function onError(m){
alert("Fout is: "+m);
}

});

The error is: object xmlhttprequest>

Please help me

+2  A: 

If you are running your app on localhost, and try to point your xml http request at another domain, you are violating the same origin policy and the browser won't actually do the request.

I am aware of 2 options:

1) Use jsonp. obviously the target server has to support jsonp
2) If you control the target domain/server, in your dev configuration make the protocol, domain, port match. This is the case where you can deploy a version of the server on localhost.

hvgotcodes
+1  A: 

so is there an answer for this issue. The reason for this request is because I like to make an chrome add-in. So the point I like to add the url to a database on a server. I doens't prefere an local database because I travel al lot

Wouter van Reeven
you shouldn't ask another question in an answer...add this question to your original post...
hvgotcodes
or as a comment to the answer :)
Steven