简体   繁体   中英

I can't to process response to a jsonp request

I have url http://translate.google.ru/translate_a/t?client=x&text=enter text&sl=en&tl=pl

If you will go through this link in response you will have js file

with text:

{"sentences":[{"trans":"wprowadzania tekstu","orig":"enter text","translit":"","src_translit":""}],"src":"en","server_time":80}

I created ajax request

function GoogleTranslateItem(sourceText, langFrom, langTo) {
        $.ajax({
            url: 'http://translate.google.ru/translate_a/t',
            data: { client: "x", text: sourceText, sl: langFrom, tl: langTo },
            dataType: 'jsonp',
            jsonpCallback: "getData",
            success: function (data) {
                alert("Success");
            }
        });

function getData(data) {
    var dataJson = data;
    alert('bingo');
}

when the answer comes from server. I can't to process it

in browser shows js error.

Syntax error at line 1 while loading:
{"sentences":[{"trans":"вход вых
------------^
expected ';', got ':'   

Linked script compilation

How can i process this response?

I think you should take a look at this ( http://javascriptweblog.wordpress.com/2010/11/29/json-and-jsonp/ )

var jsonp = {
    callbackCounter: 0,

    fetch: function(url, callback) {
        var fn = 'JSONPCallback_' + this.callbackCounter++;
        window[fn] = this.evalJSONP(callback);
        url = url.replace('=JSONPCallback', '=' + fn);

        var scriptTag = document.createElement('SCRIPT');
        scriptTag.src = url;
        document.getElementsByTagName('HEAD')[0].appendChild(scriptTag);
    },

    evalJSONP: function(callback) {
        return function(data) {
            var validJSON = false;
        if (typeof data == "string") {
            try {validJSON = JSON.parse(data);} catch (e) {
                /*invalid JSON*/}
        } else {
            validJSON = JSON.parse(JSON.stringify(data));
                window.console && console.warn(
                'response data was not a JSON string');
            }
            if (validJSON) {
                callback(validJSON);
            } else {
                throw("JSONP call returned invalid or empty JSON");
            }
        }
    }
}

The response from http://translate.google.ru/translate_a/t?client=x&text=entertext&sl=en&tl=pl i JSON, not JSON-P. Accessing JSON-data this way is against the cross-site policies, as the browsers prevent such responses to be returned to the client.

As you are allowed to include scripts from other domains, JSON-P is a way of transfering data as javascript (not JSON). You need to find an API supporting JSON-P (I'm not sure if the Translate API supports JSON-P) or create a proxy on the same domain as your client application to access the JSON data.

Read more about the JSON-P protocol here: http://json-p.org/

To create a proxy, you'll need to implement a service that fetches the content of the Translate API and reprint it in the response.

Example: /jsonProxy?text=foo Should return the contents of http://translate.google.ru/translate_a/t?client=x&text=entertext&sl=en&tl=pl

...but you won't have to access it from another domain.

我认为响应的MIME类型应为'application / json'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM