Json响应显示Content-type text / html而不是application / json

时间:2016-04-13 07:15:29

标签: json http-headers

我正在使用Postman chrome扩展程序发送帖子并获取请求并获得响应。

我收到了以下回复 -

{
  "Myresponse": {
  "ResponseStatus": 1,
  "PhoneCount": 3,
  "Page": "1",
  "PageSize": "3",
  "Phones": {
  "Phone": [
    {
      "AccountSid": "5fd7e139-080c-49ab-bbe4-7c9f686dc64e",
      "PhoneSid": "5c993989-d8cd-496b-b83c-160023247097",
      "FriendlyName": "2013554746",
      "PhoneNumber": "2013554746",
      "VoiceUrl": null,
      "VoiceMethod": null,
      "VoiceFallbackUrl": "",
      "VoiceFallbackMethod": null,
      "DateCreated": "2016-04-08 02:27:52",
      "SMSRequestUrl": null,
      "SMSMethod": null,
      "SMSFallbackUrl": null,
      "SMSFallbackMethod": null,
      "HeartbeatUrl": "",
      "HeartbeatMethod": null,
      "HangupCallback": "",
      "HangupCallbackMethod": null,
      "VoiceEnable": "",
      "SmsEnable": "true",
      "ApiVersion": "2"
    },
    {
      "AccountSid": "5fd7e139-080c-49ab-bbe4-7c9f686dc64e",
      "PhoneSid": "a3b88f36-62d7-49d1-b138-7d6d6c5950b2",
      "FriendlyName": "2014271285",
      "PhoneNumber": "2014271285",
      "VoiceUrl": null,
      "VoiceMethod": null,
      "VoiceFallbackUrl": "",
      "VoiceFallbackMethod": null,
      "DateCreated": "2016-04-08 02:27:52",
      "SMSRequestUrl": null,
      "SMSMethod": null,
      "SMSFallbackUrl": null,
      "SMSFallbackMethod": null,
      "HeartbeatUrl": "",
      "HeartbeatMethod": null,
      "HangupCallback": "",
      "HangupCallbackMethod": null,
      "VoiceEnable": "",
      "SmsEnable": "true",
      "ApiVersion": "2"
    },
    {
      "AccountSid": "5fd7e139-080c-49ab-bbe4-7c9f686dc64e",
      "PhoneSid": "670f91f5-b460-4b3b-a0d1-35e742c2a5ef",
      "FriendlyName": "2014271285",
      "PhoneNumber": "2014271285",
      "VoiceUrl": "https://76bvuptovu4g.runscope.net",
      "VoiceMethod": "GET",
      "VoiceFallbackUrl": "http://voicefallback.com",
      "VoiceFallbackMethod": "GET",
      "DateCreated": "2016-04-08 02:22:41",
      "SMSRequestUrl": "https://g62ou2lugq56.runscope.net",
      "SMSMethod": "GET",
      "SMSFallbackUrl": "https://g62ou2lugq56.runscope.net",
      "SMSFallbackMethod": "GET",
      "HeartbeatUrl": "",
      "HeartbeatMethod": "GET",
      "HangupCallback": "",
      "HangupCallbackMethod": "GET",
      "VoiceEnable": "true",
      "SmsEnable": "true",
      "ApiVersion": "2"
    }
  ]
}
 }
 }

{
"Message360": {
    "ResponseStatus": 1,
    "PhoneCount": 4,
    "Page": "1",
    "PageSize": "4",
    "Phones": {
        "Phone": [
            {
                "AccountSid": "5fd7e139-080c-49ab-bbe4-7c9f686dc64e",
                "PhoneSid": "5c993989-d8cd-496b-b83c-160023247097",
                "FriendlyName": "2013554746",
                "PhoneNumber": "2013554746",
                "VoiceUrl": null,
                "VoiceMethod": null,
                "VoiceFallbackUrl": "",
                "VoiceFallbackMethod": null,
                "DateCreated": "2016-04-08 02:27:52",
                "SMSRequestUrl": null,
                "SMSMethod": null,
                "SMSFallbackUrl": null,
                "SMSFallbackMethod": null,
                "HeartbeatUrl": "",
                "HeartbeatMethod": null,
                "HangupCallback": "",
                "HangupCallbackMethod": null,
                "VoiceEnable": "",
                "SmsEnable": "true",
                "ApiVersion": "2"
            },
            {
                "AccountSid": "5fd7e139-080c-49ab-bbe4-7c9f686dc64e",
                "PhoneSid": "a3b88f36-62d7-49d1-b138-7d6d6c5950b2",
                "FriendlyName": "2014271285",
                "PhoneNumber": "2014271285",
                "VoiceUrl": null,
                "VoiceMethod": null,
                "VoiceFallbackUrl": "",
                "VoiceFallbackMethod": null,
                "DateCreated": "2016-04-08 02:27:52",
                "SMSRequestUrl": null,
                "SMSMethod": null,
                "SMSFallbackUrl": null,
                "SMSFallbackMethod": null,
                "HeartbeatUrl": "",
                "HeartbeatMethod": null,
                "HangupCallback": "",
                "HangupCallbackMethod": null,
                "VoiceEnable": "",
                "SmsEnable": "true",
                "ApiVersion": "2"
            },
            {
                "AccountSid": "5fd7e139-080c-49ab-bbe4-7c9f686dc64e",
                "PhoneSid": "670f91f5-b460-4b3b-a0d1-35e742c2a5ef",
                "FriendlyName": "2014271285",
                "PhoneNumber": "2014271285",
                "VoiceUrl": "https://76bvuptovu4g.runscope.net",
                "VoiceMethod": "GET",
                "VoiceFallbackUrl": "http://voicefallback.com",
                "VoiceFallbackMethod": "GET",
                "DateCreated": "2016-04-08 02:22:41",
                "SMSRequestUrl": "https://g62ou2lugq56.runscope.net",
                "SMSMethod": "GET",
                "SMSFallbackUrl": "https://g62ou2lugq56.runscope.net",
                "SMSFallbackMethod": "GET",
                "HeartbeatUrl": "",
                "HeartbeatMethod": "GET",
                "HangupCallback": "",
                "HangupCallbackMethod": "GET",
                "VoiceEnable": "true",
                "SmsEnable": "true",
                "ApiVersion": "2"
            },
            {
                "AccountSid": "5fd7e139-080c-49ab-bbe4-7c9f686dc64e",
                "PhoneSid": "03488580-5f90-42f6-9ce3-ca801194d763",
                "FriendlyName": "2019481078",
                "PhoneNumber": "2019481078",
                "VoiceUrl": "http://customapps.message360.com/ytel_lead/test_answer.php",
                "VoiceMethod": "POST",
                "VoiceFallbackUrl": "",
                "VoiceFallbackMethod": "POST",
                "DateCreated": "2016-04-08 02:22:40",
                "HeartbeatUrl": "",
                "HeartbeatMethod": "POST",
                "HangupCallback": "",
                "HangupCallbackMethod": "POST",
                "VoiceEnable": "true",
                "SmsEnable": "false",
                "ApiVersion": "2"
            }
        ]
    }
}
}

当我检查两个回复的内容类型时,首先给出 application / json ,给第二个 text / html

有什么问题吗?有人可以帮帮我吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

如果您在代码下面得到了响应类型 在返回响应之前添加此

// Set header content 
if($this->requestType == 'xml'){
    $this->header('Content-Type', "application/xml; charset=UTF-8");
}else{
    $this->header('Content-Type', "application/json; charset=UTF-8");
}

答案 1 :(得分:1)

试试这个,希望这会对你有帮助..

  header("Content-Type: application/json; charset=UTF-8");