0
$.ajax({
                type: "GET",
                url: "http://api.zip-tax.com/request/v20",                    
                data: {
                    key: '1234567890',
                    postalcode: '90265',
                    format: 'json'
                },
                success: function (json) {
                    debugger;
                    // do stuff with json (in this case an array)
                    alert("Success");
                },
                error: function (a, b, c) {
                    debugger;
                    alert("Error");
                }
            });

当我运行上面的代码时,它把我扔进错误块并说

SyntaxError: invalid label
[Break On This Error]   

{"version":"v20","rCode":100,"results":[{"geoPostalCode":"902

虽然它也显示readyState=4, status=200, statusText="success"parsererror 如果我点击那个 url,我可以看到正确的 json

URL: `http://api.zip-tax.com/request/v20?key=1234567890&postalcode=90265&format=json`
Result: `{"version":"v20","rCode":100,"results":[{"geoPostalCode":"90265","geoCity":"MALIBU","geoCounty":"LOS ANGELES","geoState":"CA","taxSales":0.090000003576279,"taxUse":0.090000003576279,"txbService":"N","txbFreight":"N","stateSalesTax":0.064999997615814,"stateUseTax":0.064999997615814,"citySalesTax":0,"cityUseTax":0,"cityTaxCode":"","countySalesTax":0.0099999997764826,"countyUseTax":0.0099999997764826,"countyTaxCode":"19","districtSalesTax":0.014999999664724,"districtUseTax":0.014999999664724},{"geoPostalCode":"90265","geoCity":"PT DUME","geoCounty":"LOS ANGELES","geoState":"CA","taxSales":0.090000003576279,"taxUse":0.090000003576279,"txbService":"N","txbFreight":"N","stateSalesTax":0.064999997615814,"stateUseTax":0.064999997615814,"citySalesTax":0,"cityUseTax":0,"cityTaxCode":"","countySalesTax":0.0099999997764826,"countyUseTax":0.0099999997764826,"countyTaxCode":"19","districtSalesTax":0.014999999664724,"districtUseTax":0.014999999664724},{"geoPostalCode":"90265","geoCity":"TWAIN HARTE","geoCounty":"VENTURA","geoState":"CA","taxSales":0.075000002980232,"taxUse":0.075000002980232,"txbService":"N","txbFreight":"N","stateSalesTax":0.064999997615814,"stateUseTax":0.064999997615814,"citySalesTax":0,"cityUseTax":0,"cityTaxCode":"","countySalesTax":0.0099999997764826,"countyUseTax":0.0099999997764826,"countyTaxCode":"56","districtSalesTax":0,"districtUseTax":0}]}`

请指教。我不想编写服务器端脚本。

4

1 回答 1

2

这不是 JSONP 的工作方式。

使用正确的 JSONP,将一个<script>元素放置在页面上,并src设置为您请求的 URL。这是由 jQuery 内部处理的。

JSONP 的响应不是JSON。它是 JavaScript,调用一个函数,并将 JSON 作为参数传递。这就是允许跨域数据传输的方式,因为您只是请求 Javascript 代码。

发出请求时,您必须提供要与 JSON 响应一起调用的函数的名称,通常如下:

http://www.website.com/request/jsonp?param=value&param2=value2&callback=callbackFunc

因此,响应将采用以下格式:

callbackFunc({JSON stuff});

jQuery 在内部处理此回调内容,以便success最终调用该方法。

看起来 API 不支持 JSONP(http://www.zip-tax.com/documentation),因此您必须对自己的服务器进行 AJAX 调用,让该服务器发出您需要的请求(它返回 JSON),并在 AJAX 响应中返回 JSON。

于 2013-04-25T06:59:01.450 回答