简体   繁体   English

如何将响应解析为跨域ajax调用的json对象?

[英]How to parse response as json object for cross domain ajax call?

I'm making a cross domain ajax call. 我正在进行跨域Ajax调用。 When I heat the link directly from my browser, I get the json strin as follows: 当我直接从浏览器中加热链接时,我得到的json strin如下所示:

在此处输入图片说明

But when I'm making an ajax call to this same URL, I'm not getting the json in my response. 但是,当我对同一URL进行Ajax调用时,响应中没有得到json。 My ajax request is as follows: 我的ajax请求如下:

$.ajax({
  url: "http://172.16.201.14:801/api/apiclearing?trackNo=" + $scope.BillClearingModel.BillTrackingNo + "&&tokenNo=~Ice321",
  dataType: 'jsonp',
  success: function(response) {
    console.log(response);
    alert("Success");
  },
  error: function(response) {
    console.log(response);
    alert("Failed");
  }
});

What Im getting in console is as follows: 我进入控制台的内容如下:

在此处输入图片说明

Full Object is as follows: 完整对象如下:

在此处输入图片说明

What I'm missing here? 我在这里想念的是什么? Thanks. 谢谢。

Would you mind trying this: 您介意尝试此操作:

var datastring = { "trackNo" : "160822000037" };//try this
//var datastring = "trackNo=160822000037"; //or this

$.ajax({
            type: 'POST',//you may try the GET type
            url: "https://172.16.201.14:801/api/apiclearing", 
            dataType: 'json',// try jsonp as well
            data: datastring,
            contentType: 'application/json',
            crossDomain: true, //newly added
            success: function(data, status, jqXHR) {
                        console.log(data);
                        console.log(status);
                        console.log(JSON.stringify(data));//to string
                        alert("Success");

            },
           error:function(jqXHR, textStatus, errorThrown) {
                            alert("Status:"+ textStatus + " Error:" + errorThrown);


         }

You may consider as well to add Access-Control-Allow-Origin to your server like Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com in php 您也可以考虑将Access-Control-Allow-Origin像Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com一样添加到您的服务器Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com php中的Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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