打印AJAX响应数据(来自错误返回)

时间:2018-02-22 03:03:20

标签: jquery ajax get

我有一个AJAX请求脚本,无论出于何种原因,它返回数据,但也认为响应是错误的。我已经联系了API的创建者,他们正在调查它,但目前我正在尝试为响应创建一个解决方法。

这是我的剧本:

 <script type="text/javascript">
        $(document).on('click', '#pullDetails', function() {
             $.ajax({
                type:'GET',
                url: 'https://webservices.rrts.com/TrackWebApi/api/values/'+$('input[name=proNumber]').val(),
                success: function(data) {
                    $('.errorTitle').addClass('hidden');
                    $('.errorContent').addClass('hidden');

                    $('#carrierReturnData').removeClass('hidden');
                    $('#carrierReturnData').html(Shipment.Origin.Name);
                },
                error: function(request, status, error) {
                    console.log(request);
                    console.log(status);
                    console.log(error);
                    $('#openInvoicesOverlay').html('');
                    $('#openInvoicesOverlay').removeClass('overlay');
                    $('#freightBillDetails .box-body').html(data.SearchResults[0].SearchItem);
                }
            });
        });
</script>

现在,即使数据成功返回到我的GET请求,成功部分也不起作用。那么&#34;错误:功能......&#34;事情是有效的(尽管不应该)。

所以基于这个返回JSON:

enter image description here

我如何打印细节?与返回的SearchItem字段一样。该脚本适用于.html和.remove类,但错误的.html部分:函数不起作用,可能是因为我试图打印出响应数据。

响应更新

enter image description here

使用新响应进行更新

  

失败

     

对象{详细信息:&#34; {\&#34; SearchResults \&#34;:[{\&#34; SearchItem \&#34;:\&#34; **** \&# 34; \&#34;显示类型\&#34;:0,\&#34;一批\&#34;:{\&#34; DRAvail \&#34;:假,\&#34; ProNumber \&#34;:\&#34; **** \&#34;,\&#34; PickupNumber \&#34;:\&#34;未找到\&#34;,\&#34 ; CUSTOMERNUMBER \&#34;:\&#34; **** \&#34; \&#34; BillToAccount \&#34;:\&#34; **** \&#34 ;, \&#34; BillToNumber \&#34;:空,\&#34; BOLNumber \&#34;:\&#34; \&#34; \&#34; BOLReceived \&#34;:真,\&#34; PODReceived \&#34;:false,\&#34; PONumber \&#34;:\&#34;见下面\&#34;,\&#34; OrderNumber \&#34 ;:空,\&#34; OperationalStatus的\&#34;:空,\&#34;状态\&#34;:空,\&#34; InspectionAvailable \&#34;:假,\&#34 ; ProDateTime \&#34;:\&#34; 2018-02-13T00:00:00 \&#34; \&#34; DeliverDateTime \&#34;:\&#34; 0001-01-01T00 :00:00 \&#34;,\&#34; SpecInst1 \&#34;:\&#34; RESIDENTIAL DELY CFA 1 \&#34;,\&#34; SpecInst2 \&#34;:\ &#34; LIFTGATE DELY REQUIRED \&#34;,\&#34; SpecInst3 \&#34;:\&#34; ANY ??&#39; S CALL:CUST SERV \&#34;,\& #34;总金额\&#34;:\&#34; 145.38 \&#34; \&#34; SCAC \&#34;:\&#34; COEP \&# 34; \&#34; ReadyTimeString \&#34;:\&#34; \&#34; \&#34;饮片\&#34;:1,\&#34;重量\&#34 ;:381,\&#34; ApptDateTime \&#34;:\&#34; 0001-01-01T00:00:00 \&#34; \&#34; DeliveredDateTime \&#34;:\& #34; 0001-01-01T00:00:00 \&#34; \&#34; ProjectedDeliveryDateTime \&#34;:\&#34; 2018-02-19T00:00:00 \&#34 ;, \&#34; HAWB \&#34;:null,\&#34; Origin \&#34;:{\&#34; Name \&#34;:\&#34; ATLAS INTL \&#34 ;,\&#34;地址1 \&#34;:\&#34; 400 W WARNER AVE \&#34;,\&#34;地址2 \&#34;:\&#34; \&#34 ;,&#34; City \&#34;:\&#34; SANTA ANA \&#34;,\&#34; State \&#34;:\&#34; CA \&#34; ,\&#34;邮编\&#34;:\&#34; 92707 \&#34;},\&#34;收货人\&#34;:{\&#34;名称\&#34; :\&#34; LEANNE FRANKE \&#34;,\&#34;地址1 \&#34;:\&#34; 783 HUNTINGTON DR \&#34;,\&#34;地址2 \&#34 ;:&#34; \&#34;,\&#34; City \&#34;:\&#34; HIGHLANDS RANCHO \&#34;,&#34; State \&#34;: \&#34; CO \&#34; \&#34;邮编\&#34;:\&#34; 80126 \&#34;},\&#34; PickupStop \&#34;:空,\&#34; OriginTerminal \&#34;:{\&#34; TerminalName \&#34;:\&#34;商业\&#34],\&#34;为AuthUser \&#34; :{\&#34; CanViewBOL \&#34;:假,\&#34; CanViewInv音色\&#34;:假,\&#34; CanViewInspection \&#34;:假,\&#34; CanViewPOD \&#34;:真,\&#34; CanViewWI \&#34;:假,\&#34; FBAUserID \&#34;:\&#34; \&#34; \&#34; SharedSecret \&#34;:\&#34; \&#34; \&# 34; UserAuthenticated \&#34;:假,\&#34; WEBUSER \&#34;:{\&#34;网站\&#34;:空},\&#34; AccountGroup \&#34; :{\&#34; GroupCode \&#34;:空,\&#34; MasterAccountCollection \&#34;:空},\&#34; PermissionMask \&#34;:0,\&#34; ManualAccount \&#34;:假}}&#34; }

更新

enter image description here

1 个答案:

答案 0 :(得分:1)

你的ajax是成功的,因为它返回json而不是ajax错误响应,但你的语法不正确。

 success: function(data) {
$('#carrierReturnData').html(Shipment.Origin.Name);
},

装运未定义。

您需要的是获得成功回调的回复,即data

这意味着屏幕截图中的data ==您的JSON

 success: function(data) {
$('#carrierReturnData').html(data['SearchResults'][0]['Shipment']['Origin']['Name']);
},

<强>更新

您的数据是跨域的,您需要使用jsonp代替json。但是因为你需要先知道回调的位置(需要等待站点所有者回答),所以反而强制执行ajax跨域。我真的很喜欢这种方式。

的index.html

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
 $(function() {
         $.ajax({
            type:'POST',
            url: 'http://localhost/process.php',
            data: {num: $('input[name=proNumber]').val()},
            dataType: 'json',
            success: function(data) {
                if(data.SearchResults != undefined) console.log('success');
                else console.log('failed');
                console.log(data);
            },
            error: function(data) {
                console.log('error');
                console.log(data);
            }
        });
    });

</script>

process.php

<?php
$json = file_get_contents('https://webservices.rrts.com/TrackWebApi/api/values/'.$_POST['num']); //or you can use curl
echo $json;
?>

更新2:

我将你的json复制到json托管服务。

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<div id="carrierReturnData" ></div>
<script>
$(function(){
	$.ajax({
		url: 'https://api.jsonbin.io/b/5a8ede2aa671850974694f16/2',
		type: 'GET', 
		dataType: 'json',
		success: function(data) {
			/*
				result: 
ignore this->	AuthUser: Object { ManualAccount: false, PermissionMask: 0, UserAuthenticated: false, … }
your JSON->	details: Array [ {…} ]
ignore this->	__proto__: Object { … }

So we need to do below
			*/
			if(data.details.SearchResults != undefined) {
				console.log('success');
				console.log(data.details.SearchResults[0].Shipment.Origin.Name); //result: ATLAS INTL
				$('#carrierReturnData').html(data.details.SearchResults[0].Shipment.Origin.Name);
			}else{
				console.log('failed');
				console.log(data);
			}
			
		},
		error: function(data) {
			console.log('error');
			console.log(data);
		}
	});
	
})
</script>

jsonbin.io已启用CORS,但您的目标网站不允许使用CORS,因此我们需要使用上面提到的PHP(curl或file_get_contents)来破解它。